bacteria have the ability to mutate and rearrange their dna.
Each time bacteria is exposed to new drug, many of them die. But the few that survive have found a way to mutate and survive. As they replicate, they pass on that new ability to resist the new drug and the more they replicate the faster they can spread the new resistance to the drugs.
One has to remember that survival is the game that bacteria have been able to become master at doing for thousands of years and we are just new at finding way to kill them.