First off, we need to realize exactly what racism, nationalism, etc. are. They are both a social and biological mechanism.
First, we come to our ancestors: primates. Primates organized themselves into social groups. This was the norm for them. The moment they did this, they automatically had something in common with other members of their group. When another group appears, there is automatically a level of caution, because that group is trying to get the same things (food, water, mating, etc.) that the first group wants.
This group mentality was to protect them from things outside their group, things like predators. Over time, however, primates evolved, but the gene that made a group mentality continued to run. So, humans naturally form groups with other human beings. When they do this, they are automatically cautious and wary of people from other groups, on a subconscious level.
Next, I direct you to the monkeysphere: http://www.pointlesswasteoftime.com/monkeysphere.html
If you read the article, you will realize that humans don't just stick to one group. They are in several groups, some of which they have more in common with, and some of which they have less in common with. The former group will inspire more loyalty than the latter.
For example, if I and another American are walking around, say, Iraq, and a random Al-Qaeda terrorist attacks the other guy, I will help him, because I have a lot in common with him (we're both American and all that comes with that). In this case, I am grouping myself with him, and setting myself against the group of "terrorists".
On the other hand, if I happen to be in the vicinity of just said terrorist, and an extraterrestrial HellBeast attacks the terrorist, I will help the terrorist, not the alien. Why? Because I have more in common with the terrorist. We are of the same species, and live on the same planet.
In much the same way, we group ourselves with others who have similar interests/features/social status/etc. Our subconscious brains don't care that race doesn't exist. They simply see common features and roll with them.
On the other hand, upbringing plays a big role in this as well. Let's look at the old world, shall we? Let's take Europeans as an example. Back in the day when the churches controlled everything, and what they taught was widespread belief, Europeans began to have contact with black Africans.
During this time, it was commonly accepted that the color black and darkness were associated with Satan/evil. So, when many churchgoing Europeans saw blacks for the first time, this conditioning kicked in, and racism started busting out. Some of them had lots of self-control, and didn't show any racism. They were, however, still thinking it on a subconscious level. It can't be helped.
In much the same way, what we do is shaped by these genes, as well as our upbringing. For example, what do many racists have in common? Who did they grow up around, and who do they hang out with? Other racists of the same race!
On the other hand, when a person is raised in a multi-cultural setting that is anti-racist, they automatically group themselves with the anti-racists. Once again, we are seeing the effect of social constructs on the monkeysphere.
So, in conclusion, we are basically doing a combination of what we were exposed to growing up, and what our monkey brains are telling us to do. All of it is on a subconscious level. We can pretend not to group ourselves like that, and say "I'm not racist/nationalist/whateverist and accept everyone for who they are!!", but in the end you still think it.
I know there are going to be some people who respond to this and say "Blargh, u r a terrlbe racst OMGZ!!! I nevar do thnk bad about otharz!!!", but they will still know that what I said is a fact. Even if you don't consciously believe it, on a subconscious level, you still group yourselves, and are naturally wary of other groups.