Today ships can simply reverse the engine, use manoeuvering thrusters, or powerful tugs.
How would a sailing ship get into port or out of it against the wind.
I would assume any tow boat would face the same problem as the vessel they'd try to tow?
Toward the end of the age of sale, they would use a steam tug. There’s a wonderful painting that captures this small slice of history, called. the Fighting Tremeraire.
But I assume you’re not asking about that narrow window. You’re asking about how sailing ships would’ve done this before they could get assists from a steam tug.
The answer is always it depends, but I’ll give you a few examples of how the wind would’ve affected the vessels’ ability to enter, or leave port.
First, there’s always the chance that adverse weather could, in fact, prevent sailing ships from entering or leaving a port at all. There are many times in history, when voyages or invasions were postponed due to the wind simply blowing in the wrong direction. Julius Caesar was delayed for weeks crossing over to Britain for example.
If the difficulty is very local, and the vessels can reach the harbor or, leaving the harbor, expect they can make way to their destination, then sailing ships could use several methods for maneuvering to and from the pier.
Smaller craft could be rowed or sculled.
Some larger ships had provisions for sweeps (long oars) which would be used in the absence of wind, for short distances.
Ships of quite substantial size could be moved along under tow from smaller rowed boats.
A small boat could carry a line from the ship to the shore, or vice versa. This would enable the crew to use that line to pull a sailing ship into its position against a pier.
Kedging refers to a process where an anchor is used to drag a ship to a specific location. The heavy anchor is carefully loaded into a boat, which is rowed into position in the direction the ship wishes to go. The anchor is dropped at that position. The crew on board the ship, pulls against the anchor, using the winch, moving the ship towards the anchor. The anchor can then be raised, and the operation repeated. The success of this depends upon the conditions and the skill of the crew. Although, of course, it would be most straightforward in calm weather, it was use at times to pull a ship off of rocks, or a reef, or to prevent it from getting pushed further into the shore during a storm. Kedging in that kind of weather was quite a heroic act.
In addition to the wind, many ports were on estuaries or rivers. The state of flow of the river and the tides was important. If the wind was adverse, but the current was favorable, the ship could be guided into the proper current and sales kept furled, so that the wind had minimal influence, and the current would move the ship in the proper direction. Ports on tidal estuaries or tidally-influenced rivers, would sometimes see ships spend half a day drifting in the proper direction, and then anchoring or tying two trees ashore when the current reversed as the tide changed.