In addition to the things pohnpei397 explained, I would add that nationalism is in many ways a logical extension of human behaviors.
Many people function in social settings according to tribal patterns. This is a flexible definition that refers to the ways we categorize our beliefs, appearances and so on. If you consider who you see associating with whom in public, you'll generally notice that they share common features more often than not, especially in groups. Humans naturally gravitate toward these group dynamics, possibly because from an evolutionary standpoint it makes sense to team up with the people that share your lifestyle.
Nationalism is like making the entire country your tribe, especially when discussing things in terms of international groups. You'll notice that people from different parts of a country will probably identify with different groups, but these differences are less important than their identities as Americans/Canadians/etc. (Notice how often "Americans" are discussed in international media, as opposed to, say, "white middle-class liberal Californians", or in the case of anti-American sentiments, rarely do they specify that they aren't targeting demographics X Y and Z, but rather Americans as a whole). Nationalism maintains group cohesion; it establishes an "us" and a "them" which allows both sides to better negotiate their relationship and dictate group behaviors.
Nationalism can be beneficial because it is very similar to patriotism. Nationalists have pride in their country and/or their group of people. This can be a very good thing because it can lead people to work hard to improve their country/group. A group of people that does not think well of themselves may lack morale. They may not wish to work for the benefit of the group because they are not proud of that group/nation. If they feel pride, they may well do more to try to maintain the country/group status of which they are so proud. Nationalism, then, can motivate people to improve their country or the status of their group.