When making yogurt, why doesn't bad bacteria grow as well?
The initial heating of the milk, besides denaturing proteins to improve the texture, also pasteurizes the milk.

The culture needs to be added in a high enough concentration to crowd out harmful bacteria that might exist.

That said, if your tools or containers are dirty or if your starter is dead, or you don't add enough starter, your yogurt can grow accidental bacteria and go off.

Note
As Bob1 commented, this applies to the first few hours of fermentation before the milk has become acidic enough to inhibit other microbes.