Many theories of intergroup relations in social psychology try to explain this phenomenon. Ingroups are groups to which a person belongs, and outgroups are groups to which a person does not belong (and which could therefore become target for ingroup bias). There is an almost infinite number of groups to which a person belongs, depending on how he or she categorizes the social world.
Self-imaging plays an important role in the initial exploratory stages when people become members of a new group. . Because of the centrality of the self in social perception.
Either they contributed in a way that would not affect the payoffs of outgroup members or in a way that would also decrease the payoffs of the other group. It turned out that contributors hardly chose the option that harmed outgroup members, providing first support for the claim that subjects did not intend to decrease payoffs of outgroup members. In a more elaborate experimental design Abbink et al. found contributions to the group public good to be substantially above the Nash equilibrium prediction (which was strictly positive in their design). Furthermore, the authors reported that there were no significant effects of outgroup past behavior on individuals’ contributions and conjectured that “subjects seem to focus more on the interaction with the other team members than on that with the rival team, but at this point one can only speculate whether this can be generalized and how this is best explained” [Abbink K, 2010].
Closeness to the in-group, which can be triggered simply by labelling, seems to be the main driving force for in-group favouritism, whilst out-group discrimination is determined by social distance, conflict, and competition between groups.
"In-group love" and "out-group hate", Halevy N, Bornstein G, Sagiv L, Psychol Sci. 2008 Apr; 19(4):405-11.
Abbink K, Brandts J, Herrmann B, Orzen H (2010) Intergroup Conflict and Intra-Group Punishment in an Experimental Contest Game. Am Econ Rev 100: 420–447.