The study of the immune system and its interactions with pathogens, allergens, and other foreign substances.