How does the body know which bacteria friendly and unfriendly? And, is there a certain amount of bad bacteria or viruses that are good for the body to experience to build up a tolerance to, e.g, if someone sneezes near you do you either get the illness or not, or can a little bit of exposure be a good thing?

I think our body doesn't distinguish bad or good bacteria. It's a body's reaction that could be good or bad. The matter is in a quantity of those bacterias. When we do vaccination we transfer some bad viruses to our body and it is actually to build up a tolerance.

There's only one thing an organism "wants to do" is reproduce if it can do that best in a living body it will not wish to kill you but some bacteria want you dead so that it can spread out and find other hosts.Many of the bacteria that live in the stomach or intestines are quite happy with that environment and cause no symptoms but some are looking for other hosts so they cause Diarrhea so they can find more hosts.