The process that generates immune cells tests them for autoimmune reactivity and aggressively culls ones that are.
https://en.wikipedia.org/wiki/Thymus#Function
The Thymus (Which someone else mentioned) effectively checks the following:
(1) Does the cell react to an antigen (Foreign particle) [β-selection]
(2) Does the cell still work with the immune system (Can it bind to the cells it needs to bind to once it finds the antigen) [Positive Selection]
(3) Does the cell recognize only foreign particles and not the body itself [Central Tolerance / Negative Selection]
I had the misfortune of blowing out my right knee and acquiring a chlamydia infection ~simultaneously. And then developed acute rheumatoid arthritis. Which has progressed, albeit slowly, ever since.
So it goes.
The process that generates immune cells tests them for autoimmune reactivity and aggressively culls ones that are.