Many ways you could a semester of immunology on this. High a level there are molecules called MHC (Major Histocompatability Complex), or HLA (Human Leukocyte Antigen).
They are very important to the immune system in a number of ways. Any individual has a few sets of these, and there are many different genetic types of these molecules so the differences between any two people can be many. The immune system is even sensitive to their absence, since their down regulation is a strategy for some pathogens such as virus.
Anyway, as the name MHC suggests, this is one way that the immune system detects foreign tissue.