Immune function is likely to be a critical determinant of an organism's fitness, yet most natural animal and plant populations exhibit tremendous genetic variation for immune traits. Accumulating evidence suggests that environmental heterogeneity may retard the long-term efficiency of natural selection and even maintain polymorphism, provided alternative host genotypes are favoured under different environmental conditions. 'Environment' in this context refers to abiotic factors such as ambient temperature or availability of nutrient resources, genetic diversity of pathogens or competing physiological demands on the host. These factors are generally controlled in laboratory experiments measuring immune performance, but variation in them is likely to be very important in the evolution of resistance to infection. Here, we review some of the literature emphasizing the complexity of natural selection on immunity. Our aim is to describe how environmental and genetic heterogeneities, often excluded from experimentation as 'noise', may determine the evolutionary potential of populations or the potential for interacting species to coevolve.