To keep it simple:
For one it's just competition. If you have regular contact to a diverse set of microorganisms chances are higher that they will keep each other populations in check. The dose makes the poison applies here too in some narrow sense.
Additionally (or even mainly) our immune system always on guard (me/not me) gets to play around with the obvious stuff and not be overwhelmed or be "bored" but constantly trained/tamed. We have evolved to have constant contact to a diverse set of microorganisms which modulate our immune system e.g. through toll-like receptors.
As an extreme case some people develop some natural immunity to the tetanus toxin through regular exposure to a extreme variety of microorganisms, obviously this isn't reliable nor a gamble remotely worth the risk.
[0]https://pubmed.ncbi.nlm.nih.gov/3980089/
[1]https://pubmed.ncbi.nlm.nih.gov/6827147/
Edit: don't know why OP gets downvoted it's a perfectly fine entrance question/bafflement to motivate some deeper digging into microbiology/immunology.