This question is better suited for an anthropologist or sociologist than a historian. The reason being that this question is less rooted in historical study but rather sociological or anthropological analysis. Historians try to deal with specific instances or subjects and, usually, broad questions of human nature are outside our purview. For instance, you could ask "Why was the Third Reich racist?" and we could address that as you name a specific entity within a time frame. But something as varying as racism over thousands of years of recorded human history is too broad for a good answer.
Some time ago I assembled a list of earlier posts about the history of racism, which I can post here below:
This you can read, especially the last two, while waiting for a new response from one of our users.