Pre-modern bakers and brewers kept live cultures of yeasts harvested from the environment. Or they simply let wild yeasts grow in the batch as it happened.
For dough you can use various sourdough starter techniques. There are many traditional techniques from different parts of the world but most of them involve keeping old, uncooked dough around from a previous batch to use in the next batch. Or keeping a culture that is maintained separately from making the dough. The difference being that in one you're simply saving some of the old dough for the future, while in the other you're keeping a culture that you're intentionally feeding over time (which may use essentially a dough recipe). One reason to use the separate culture technique is that it's often beneficial to avoid having salt in your culture. Both techniques have been used for hundreds of years, using old dough has been used for at least thousands of years.
For bread there is a time critical component involved because you're not just fermenting for the sake of it, you're using fermentation to add flavor and change the texture of the bread (through CO2 bubbles, etc.) For fermented foods such as wine, beer, pickles, etc. it's not always necessary to maintain a separate culture of yeast. If you process the ingredients properly then you can make use of natural yeast from the air and set things up so that much more often than not the environmental yeast that you want to make use of comes out as the "winner" in your batch. This can be a bit easier for things like wine where the acidity gives the yeast a big starting advantage, or things like fermented cabbage or vegetables where the salt also gives the yeast an advantage, over other microbes. Nevertheless, as with bread the technique of simply using a sample of old "good" beer or wine to inoculate a new batch has been practiced for centuries.
If you leave a mixture of flour and water at room temperature for 24 hours, it will collect wild yeast from the atmosphere and start fermenting. It won't be enough to leaven bread and make it rise, but the tangy taste of sourdough is quite detectable in 24 hours.
If you leave it for a week, it'll bubble and froth like crazy, and you can start removing portions to leaven bread for baking. Of course, you have to keep replenishing flour and water to make up for what you remove.
This is called sourdough starter, and it lasts indefinitely. There are starters around that have been going for over a century.
This is what bakers did in the past before there was commercial yeast. Each baker bred some starter in a corner of the kitchen or pantry, extracted some portion for the day's baking, replenished the starter with an equivalent amount of flour and water.
There are hundreds of strains of wild yeast, so which one dominates in your kitchen is a matter of happenstance. All will leaven btead, but the speed at which they work can vary. This is not a problem in home baking where instructions are typically like "let the dough sit until it doubles in size, however long that takes".
But in commercial bakeries, you want something fast and consistent. So over time, as thousands of bakers used their own starters, they began to share starters that were particularly fast growing. This is how commercial yeast originated.
Of course, commercial yeast isn't an essential ingredient in baking, only a convenient one. You can leaven bread just fine with whatever random yeast already exists in the air in your kitchen. Many bakers prefer these wild types -- they may be slow, but they can produce richer, more complex flavors. Another difference is that starter made at home contains a bunch of fermenting bacteria in addition to yeast, for even more complex flavors.
Contrary to what some people think, brewers yeast and bakers yeast aren't identical. So the theory that bakers just borrowed yeast from brewers isn't very tenable. Sure, it'll leaven bread too, but centuries of selection have optimized each type for its particular job.