The force on a sailboat due to the wind, is always at right-angles to the sail. Even if the incident wind is partly against the direction of travel of the boat, by keeping the sail close to the line of the boat’s keel, the sail can still "catch" some of the wind, and there is still an element of the force on the sail in the (forward) direction of the boat. There is also a (large) element of the force to the side (at right angles to the keel). So the helmsman will need to tack (i.e. swap the sail from one side of the boat to the other and back again) occasionally to maintain the required direction straight ahead.