Bakers used to keep some of the dough as a starter for next day bread. That dough contained the ferment that promote the rise of the next bread.
But the first day the bakers had to start from scratch and then is when the environment came to their avail.
The air contains billions of spores of wild yeast as well as bacteria of any sort. These tiny creatures are everywhere, on the table, on the tools, even in the water and, sure enough, in the flour. But together with them many other not so useful or nice to the baker are also ubiquitous.
When the baker mixed water and flour the first day he also included a huge variety of spores and bacteria in his batch. The kneading included them in the dough putting them in contact with a strange environment made of water, starch, some enzymes, some tiny bubbles of air kept together by a lattice of proteins.
That environment was the arena for scores of microorganisms to thrive, compete and win or perish according to their capability to nourish of the dough and manage with nearly no oxygen. It took time for some to win and colonise the entire mass but in a few days and with fresh supplies of water and flour every now and then they built a composite colony of yeast and bacteria species capable to digest the dough like a single super organism.
Then like now yeast use an enzyme called amylase to decompose starch in simple sugar. Amylase is also in the flour, and before in the grains of wheat, ready to be activated by humidity when the grain starts germinating to nourish the sprout still unable to synthesise sugar on its own. And also some bacteria have the capability to produce amylase, developed or captured from other organisms sometime in its evolution. By the way, amylase is what our mouth produce in the saliva that enables us digesting starch.
To get started spores and bacteria need to rely on a small supply of amylase already in the flour to find sugar to support their life. When the concentration of sugar, the degree of humidity and temperature reaches a threshold then one by one, at different levels, the different microorganism capable to survive in the environment turn from a dormant status to active and start eating and reproducing.
Some have very fast reproduction cycle, some slower. Some eats more some less. Some tolerates more extreme conditions some not. This of course is very important because all these living being also produce a lot of waste, heat, and depletion of the resources. Some produces alcohol as waste, the yeast, some lactic acid, the bacteria using an anaerobic metabolic process similar to the one occurring in our muscles when exercising that leaves them sore with lactic acid.
Some bacteria even manages to use the alcohol waste to nourish and produce acetic acid. And they all release water and CO² that remains trapped in the lattice of the dough forming the bubbles that make the mass rise to more than twice its volume.
All in all it is an extremely complex interaction made of rather simple lifestyles that despite the enormous number of variables and possible interrelations between them produce with high reliability the same outcome: the dough turns into a soft puffed sponge of dough and gas bubbles, with a characteristic slight scent of milk, vinegar, beer, even with tones of fruit, nuts and sweet spices. The taste is slightly sour and the smell and taste of flour nearly disappears.
Of course sour dough develops differently in different conditions and places, and using different flours and even different water, but it definitely reaches its balance within a quite narrow range of variability. This happens thanks to a few main interactions withing the microorganisms, their metabolic sequences, and the environment with its changes along the maturation.
Nowadays instead of using some bread dough from the previous batch as a starter the bakers that use sour dough tend to keep it as a separate batch from the main bread dough. They nourish it every day with a same quantity of flour and half quantity of water maintaining an hydration round 50% to 55%. The process is also called "refreshing" the sour dough.
They pick the quantity they need for the bread and then they proceed with the refreshing to have the sour dough ready the next day. Of course there is quite some art in managing properly the sour dough.
First of all the quantity needed may vary day by day and then the baker need to manage very carefully the refreshes making them more or less frequent to compensate the and prepare for the expected consumption without producing too much leftover.
Considering that with each refresh the sour dough becomes 2.5 more than the initial quantity it is quite clear that a baker who keeps refreshing a too big initial quantity of sour dough without disposing of the excess part will end up very soon with tons of sour dough covering the entire neighbourhood.
Sour dough, like any living thing, is subject to diseases too. It might turn too acid, too weak, too strong, or even contract real infections when "bad" bacteria or microorganisms have the upper hand or some of the good guys turns bad due to natural mutations or external events.
As you see then the Italian name "pasta madre" (mother dough) is a very well deserved one for this remarkable little thing of a sour dough.