It goes back to prehistoric times, when tribes had to distinguish between one another, so that their precious food wasn't stolen away.
So they learnt to delineate one tribe's members from the others, which would have impacted upon their brain processes and evolved along the natural selection route.
That's why humans etc learnt to categorise people into boxes, using stereotypes as a form of shorthand to judge others.
It's quite clever what the brain can do when pushed to it. You wonder at times that, should there be a God(good question, should there?), then what kind of brain structure would 'He' have, to have created what must evidently be a 'pared down' version of his own in human form...?
Blimey, what thoughts must go through his brain, and how do they present themselves to him? Imagine him doing the equivalent of a 'Hamlet' moment? 'To be or not be...?' What a fascinating guest he would be at a dinner party...?!