First of all, the Plasmodium falciparum parasite that causes malaria is neither a virus nor a bacterium. It is a protozoan, which means that it has membranous organelles inside, such as a nucleus and mitochondria.
For this sort of evolution, it is important to remember that P. falciparum only spends a small portion of its life cycle in the human body. It spends a good portion of it in a mosquito's gut.
During the reproduction of the parasite, mutations occur randomly. Most (>99.9%) of these will cause no effect or will be harmful to the parasite. A small number of these mutations may, however, confer some resistance to the anti-malarial drugs. If a patient with malaria does not take a complete course of the drugs, then the parasites that survive will be those who were more resistant to the drugs. Those will survive to be transmitted by another mosquito. It's important to understand that the drugs don't make the bugs evolve resistance, per se. But that without the drugs, there is no selective advantage to the bugs that are resistant (and resistance usually has some disadvantages associated with it, too). Since the drugs offer a selective advantage to mutants that have full or partial resistance, the population of parasites will become enriched in those resistant mutants and the susceptible mutants will be killed.
Let's take an example of someone with a bacterial infection, since it's easier to describe:
They start with an infection caused by ten trillion bacteria, all of the same type and reproducing. With each round of bacterial reproduction (about 20 minutes) mutations occur. Some of these mutants will be slightly resistant to the antibiotic that we prescribed, just by sheer chance. Remember, it only has to happen once in 10 trillion. That resistant mutant will survive longer while the patient is taking the medication than its susceptible brethren.
If the patient finishes the entire course of antibiotics, the partially resistant mutant will still be killed, but if the patient stops early, the partially resistant mutant is more likely than the susceptible bugs to be alive. And now that partially resistant mutant can be spread on to another patient who will also not finish their antibiotics. After a few rounds of non-compliant patients, that partially resistant mutant will have mutated into a fully resistant mutant, or MRSA, which is exactly how MRSA appeared in the first place.
It's a lot easier to describe with pictures.
"The sky calls to us. If we do not destroy ourselves, we will one day venture to the stars."