The study of the genetic basis of the immune system and its response to pathogens and other environmental challenges.