Because humans are tribal. Which is both an evolutionary thing and a scientific fuck up. The science part was the idea to separate different humans into races. As a species we are the same. But scientific thinking split us up and we've been dealing with that ever since.
There is an odd thing that affects humans. Generally we dont like our local societies to change (street, neighborhoods, villages, towns) and fear change when it becomes city and country wide.
We dont like new houses being built, new types of people moving next door to us. Nice areas protest and "non nice" developments anywhere near them. etc. etc.
This is, when you really think about it, all very odd. We are only on this planet 80 odd years. Humans have been around for over 100,000+ years. The societies, social structures, tribalism, inordinate pride in coming from a certain piece of geography, that is held so dear by so many people is actually a relatively new thing. Nation states, with national identities are only a few hundred years old.
Which then makes differences very real. Different ideas, different values, different approaches to life. And humans find it hard to accept differences. And dont want those differences near them and use racism to reject and be very clear about those rejections.
Of course, being on a planet of 6bn people (and growing) rejecting differences in other humans and wanting society to never change is impossible.
Instead of racism we need to actually look at how everyone can agree common sets of basic values and basic behaviors where we can share the same spaces without falling out.
But that is harder than being racist so we tend to do that instead.