Exercising and eating well are good habits to get into...
In Africa,HIV/AIDS and Malaria are common diseases so a Malaria vaccination and the use of condoms will stop you from getting them.
... but getting vaccinations and using a condom while having sex will stop you from getting pathogens.
In our blood stream, we have Phagocytes and Lymphocytes (part of the specific immune system that combats certain pathogens). Lymphocytes produce antibodies that latch onto the pathogen. On the first infection it takes a while for them to make the right antibody for the pathogen.These antibodies are there to flag the Phagocytes that the pathogen it is attached onto needs to be eaten. Phagocytes swallow the pathogen and then they break it down with acids and enzymes. After the pathogen is broken down, the phagocyte puts the remains back into the blood stream as it is not harmful anymore.
Vaccines take advantage of this. All the vaccination is just dead or inactive pathogens that acts as the first infection so when you get an actual infection, the antibodies are easily created for when it comes.
After the first infection, some lymphocytes remember the infection so when the infection comes again, they will have the right antibodies for the pathogen so the infection would be stopped quicker.
In countries like the US and the UK, there are immunity programs that try to vaccinate the population. These programs stop the spread of a certain disease like chicken pox. If one or two people are vaccinated in a group of 100 people, 98 people will have the disease but if 98 of the 100 people get vaccinated, then the disease will stop to spread. This is called herd immunity.