Great question. A frequently ignored fact is that our immune system is very very good at protecting us against any type of infection, and will kill most. This is why we don’t have to worry about most bacteria, viruses etc. in the environment. They are rapidly killed. However a small number of parasites has evolved to cope with our immune system. Take two examples: dog hookworms (Ancylostoma caninum) are not able to establish an infection in humans, but will induce a massive response which may lead to the destruction of a lot of tissue, whereas Ancylostoma duodenalis (the human hookworm) can be almost asymptomatic in the gut, meaning that you have it, but it causes very little symptoms. Another example is the pig gut roundworm (Ascaris suum) and the human roundworm (Ascaris lumbricoides); the two worms look identical, and it is really difficult to tell them apart, but only one infects humans (I’ll let you guess which one!).
There are about 300 worms which can infect humans.
The answer to your question ‘how’ parasites can adapt is very difficult: in a nutshell, through mutation and adaptation, the two forces underlying evolution. Which mechanisms specifically allow parasites to survive are very specific, depending on the host and the parasite, and very ‘technical’ , they are a combination of hiding from the immune system (called immune evasion) and blocking some parts of the immune response (called immune suppression). The branch of parasitology that studies such processes is called ‘Immunoparasitology’.
As many as the types of parasites, there are also many ways different parasites resist the natural defences. And adding to Franco’s answer, I would give examples of my “favourite” parasites!
Little parasitic worms living in human blood, also-known-as blood fluke, and also-known-as(!) Schistosoma mansoni, they live in the blood where components of immune system e.g. white blood cells are flushing about detecting things that are “non-self”. The worms coat themselves with molecules from within the blood and use them like an invisible cloak hiding themselves from the detection by the immune system. But the worms are also master of immune modulator and they can also secreted things to alter the immune response. Many other worms do this immune modulator too.
Another of my favourite is TRypanosoma brucei, little protozoa parasites, also live in blood. This one do not use the invisible cloak strategy but they keep changing their coat, such that when the body recognise the coat and mount up immune response to get rid of the parasites, some of them managed to switch to a different coat and escape the immune attack…essentially it’s like they are running away from the immune system. The problem is they have over 1000 different coats and they can eventually reach the part where it’s difficult to get rid off.
And I should add that this is a big picture of how they do it; exactly what molecular mechanisms they use to succeed is still an active area of research.