You must have heard of the classic stories of pathogen and insect natural selection. Viruses like HIV attacked by white blood cells and medicines and insects that feed on plants sprayed with insecticide evolve to gain immunity in incremental steps. Simple variation allows some pathogens or insects to be better adapted every step of the way. The beginning population has mutations that turn out to be beneficial. It's circumstance that decides whether the resistance genes are beneficial.