Yes, it’s possible (and common) for a boat to sail against the wind by the power of its sail alone. Sailors have known this for hundreds of years but if it sounds impossible, no worries, I thought so too at first.
Here’s how it works, simplified so I can get in trouble with pedants:
The boat, first of all, has a keel (the blade-like bottom of the hull) which “locks” it into movement along a single axis: forward and backward. The wind is not going to blow the boat sideways, at least not very effectively.
Second, sails are curved, not flat, and can rotate (when seen from above). The force created when the wind deflects off the sail matches the curve of the sail, more or less.
So if the wind is blowing directly south ⬇️ and you want to travel north ⬆️ you angle the boat northeast ↗️ so it can only move northeast or southwest. Then you point the sail east ➡️ so the wind gets deflected west ↩️. Newton’s third law does the rest. When the wind hits, the boat will move northeast ↗️ because the keel prevents it from going straight east.
Then after a while you turn the boat (“tack”) northwest ↖️, point the sail west ⬅️, and continue.