Letting a bowl of flour and water go bad in a warm corner seems problematic at best.
I'm willing to believe there is some process that goes on to limit pathogens, but reading around this doesn't seem to even be considered.
The hint is in the name, sourdough. Per the University of Wisconsin Extension:
Properly prepared starters are safe because they become acidic due to the fermentation action of lactic acid-forming bacteria present in the mixture. These bacteria and the acid environment formed inhibit the growth of other bacteria, but do allow yeast, if added, to grow and help leaven bread products.
Still, they point out care must be taken to get a good result. There have been food safety incidents related to home made starters, and using starters that can be stored in the refrigerator is recommended.
Please see the linked article for more detail.
There is a difference between letting something "go bad" and fermenting it. Fermenting is an intentional process by which certain (desirable) bacteria / fungi "eat" the food. The "waste" products of those microbes can be very valuable for humans, be it because it adds flavor, nutrients such as vitamins or because of other properties (such as the leavening of bread). Usually it's a combination of the above. If something spoils, however, the food is contaminated with undesired or even dangerous bacteria.
Sourdough in particular isn't that different from bread that's leavened with only yeast. Yeast is a fungus. The only difference is that sourdough also contains lactic acid bacteria (which are also in your intestines, the air and on most veggies. It's probably one of the most common bacteria there is). Those bacteria are the reason sourdough doesn't spoil easily, by the way.
For millennia, fermentation was one of the most important ways to preserve food (sauerkraut for instance) or bake bread (sourdough, Ancient Egyptian times). Even salami is fermented. Combined with refrigeration, such food can last even longer. Sauerkraut doesn't spoil for 6 months, easily.
Long story short: there are a great many microbes that are very important and beneficial to humans. Fermented food is as safe as any other food, given that it was fermented properly. As for sourdough: it maintains a very stable culture of lactic acid bacteria and yeast, effectively preserving the dough for as long as they don't die (and even then, the lactic acid slows the growth of pathogens considerably).