This reminds me a bit of something I read about why dictatorships, and corruption, are so common world-wide. I forgot the name but there was a book on it.
The basic idea was that when essential services are lacking and not guaranteed for everyone, such as healthcare and education you must sacrifice your integrity in order to secure those for your loved ones.
So, sure, behind all this can be plain crab-mentality, racism and so on. But I also imagine a significant factor can be attributed to fear of losing something seen as essential and not guaranteed.