When a germ such as a virus or bacteria enters your body, your immune system goes into battle. It makes antibodies that locate the germ and launches an attack to fight it off.
After the antibodies have attacked they stick around in the body to protect you if the same germ enters your body again.
Often the antibodies can stop an infection by the remembered germ should it enter your body again. The infection is stopped before you even show signs of being sick!
Unfortunately these antibodies are very picky and only fight the germs they were made to fight.
Vaccination works by giving you a weakened or dead form of certain germs. Because they have been weakened or killed they are not strong enough to make you sick, however they are still strong enough to make antibodies to fight the germ off.
Antibodies made from the weakened or killed germs stay in the body ready to attack should that specific germ ever enter the body.