I'd like to illustrate this question with some detail on particular types of ship or boat.
The classic image of the Age of Sail is a tall ship with three masts, each festooned with square sails hanging from yard-arms. This "full ship rig" was used on vessels as small as HMS Bounty, up to comparatively vast line-of-battle ships, galleons, and the famous tea-clippers of the mid 19th century. It provides the maximum sail area for a given height of mast, along with some flexibility for trimming and manoeuvring the ship with the aid of the wind. As long as the wind is in a favourable direction (run or reach), a square-rigged ship can outrun any other sail type, all else being equal.
However, as other commentary notes, the square sail is very bad for sailing into the wind. The reason for this is the lateral stays, which run either side of the mast to help keep it upright, but physically limit the angle to which the yards can be braced round. This is the origin of the term "close hauled"; the lee braces would be hauled as tight as possible in order to get every last quarter-point of upwind course. Even with this, many ships made so much leeway that their actual course through the water was roughly perpendicular to the wind, especially in bad weather.
NB: full-rigged ships did have a number of jibs, staysails, and a fore-and-aft "spanker" on the mizzen (rearmost) mast. These could be hauled much closer in than the square sails, but were insufficient in total area to make the ship sail upwind by themselves. They were provided mostly for trimming the ship and aiding turns; backing the jibs was essential for completing a "tack" while the square sails were blown back against the masts!
So a great deal of trade planning and naval tactics of the time relied on travelling with the wind, rather than against it. Rounding Cape Horn in the "wrong" direction was extremely difficult, though a well-handled naval frigate could do so with reasonable confidence in a suitable season.
As trade patterns changed though the late 19th and early 20th centuries (due largely to the introduction of steamships), sailing ships often changed rigs to include fewer square sails and more fore-and-aft sails, resulting in large ships carrying barquentine, brigantine and even schooner rigs. Schooner rig refers to any case where the main sails on all masts are fore-and-aft, there is more than one mast, and the foremast is not taller than the other(s); there were schooner-rigged ships with as many as six or seven masts.
This had two important advantages for such ships: first, they could now sail closer to the wind and therefore had more flexibility in choosing trade routes; and second, fore-and-aft sails could be handled by fewer men, saving money and thus helping to keep the ship profitable. The downside was that they were no longer quite as fast when sailing downwind.
Smaller ships and boats began using fore-and-aft rigs much earlier, because their duties were not compatible with sailing down the reliable trade-winds or even waiting for the wind to change in their favour, hence efficiently sailing upwind was very important. Since I actually have experience sailing these (principally a rather worn-out Mirror dinghy), I can describe what happens when sailing close-hauled.
The description in Wikipedia of "as close to the wind as allows the sail(s) to generate maximum lift" is technically correct, but possibly misleading. Many boats are fitted with tell-tales on the sail to indicate when they are correctly trimmed and producing maximum lift; when sailing upwind, a common technique is to steer by these tell-tales (with the sheet hauled in tight) instead of trimming the sail by them as usual.
What must be remembered is that with the sail almost parallel to the boat's axis (as is usually possible with a fore-and-aft rig), the lift it produces is mostly sideways and only slightly forwards. That strong sideways force must be countered by the crew hanging off the weather gunwale to keep the boat upright, and by the keel or centreboard resisting any tendency to drift to leeward.
Also, the fact that the boat is moving forwards shifts the apparent wind direction forwards, so you are technically sailing even closer-hauled than your heading relative to the true wind would suggest. This is a large part of the reason why you lose speed when trying to point higher - but you can often take advantage of a gust, which will tend to move the apparent wind back towards the true direction, by pointing higher during it. This requires quick reflexes!
The age and condition of the sail also matters; canvas tends to stretch over time, spoiling the sail's intended shape and making it less efficient. Our Mirror's sail had seen significant use when we bought the boat (the numbers had all fallen off due to the glue being contaminated with sand), and we never replaced it, so it must have been quite distorted. We also had no tell-tales, but it was quite noticeable that one part of the sail would start to invert before the rest when we tried to "pinch" too much, and it was possible to steer by that. We certainly wouldn't have won any races in that boat, even against other Mirrors.
Many newer and more sophisticated boats have sails made of Terylene - yes, the same material used in the waterproof layer of reusable nappies. The main advantage, I understand, is that it doesn't stretch as much as canvas, so holds its shape better. They are easily identifiable by the loud bang the sail makes when it fills.
An interesting variation on upwind technique is seen with traditional Norfolk wherries, of which only a few are still in sailing condition. These boats are specialised for carrying cargoes up and down the narrow rivers of the Norfolk Broads region - without the aid of horses and towpaths, and while dodging other river traffic to boot. I was privileged to ride on one under sail some twenty years ago, and it made quite an impression.
The wherry is characterised by a very large and tall gaff sail to reach the undisturbed air above the surrounding vegetation, and in that video you can see how narrow most of the rivers it has to sail down are. Most of the time, it is possible to simply sail down the river while trimming the sail to suit, but occasionally the wind blows directly down a river reach, and then it is necessary to tack.
In such a narrow river, conventional tacking would see the wherry spending most of its time changing course. What they do instead is to tack across the river one way, building up speed in the process, then point the boat directly up the river and use the momentum gained. A few seconds later, they'll tack across the other way, making use of a huge rudder to maintain precise control, and repeat the process. Carefully timing these manoeuvres is also required to pass other traffic safely.