Racism is a flawed application of the in-group, out-group capacity of our minds. Those in your in-group look, speak, believe, screw, or think like you; the opposite for the out-group. Additionally, humans gain a preference for familiar settings. If someone has only been exposed to one type of environment for their entire life, then they (generally) automatically gain an aversion to other stimuli. (Look up developmental psych, and learning if you want to know more.)
I'll use Dragon Age as an example. There are Dwarves who are afraid of the surface because they've only known how to live underground. It's not because they are Dwarves; after a generation, aversion to the surface disappears. The writers applied real, honest-to-goodness psychology here.
For another example, lets take someone who's only lived in the Great Plains, in a socially, ethnically, religiously, and economically homogeneous environment. Blindfold that person, transport them to the center of Taipei, un-bindfold them, and you have yourself an instant panic attack. Our victim here simply cannot process the number of people, the terrain, the buildings, the language, the people.
If someone is only exposed to a few type of people, they will count those people in their in-group. If you expose someone to a broader group of people, they will be more likely to consider more types of people in their in-group.
Racism is the product of a bug in our ability to differentiate between different types of stimuli that only comes about though intentional or accidental nurturing.