The fundamental principle here is that of antigens. Antigens are molecules which are present on all cells and vary widely according to what kind of cell is expressing them. read more
In the Immune system, when the B cell and T cells mature there is a process known as a Central Tolerance, where auto-reactive cells, which can attack self antigens are selectively eliminated. Similar process also happen in case of peripheral tolerance. read more
Similarly, higher organisms have also acquired their immune system through evolution that discriminates nonself pathogens and self-body to protect their lives from pathogens such as bacteria or viruses. The brain system may distinguish integrated images of self and nonself created from many inputs, such as vision, sound, smell, and others. read more