A vaccine is a small amount of a dead or inactive pathogen that is injected into the patient to make sure that the body realises that it is a foreign body and produces antigens to destroy the pathogen.
The body recognises the pathogen as a foreign body and begins to find the correct antigen that will lock onto the pathogen and destroy it. This process takes a bit of time and if the pathogens had been living it would have made the patient feel ill by now. The body produces lots of replicas of the antigen and a memory cell so that the antigens can be made again quickly should the pathogen enter the body again.
This is a diagram of a white blood cell and a pathogen. When the pathogen enters the body the white blood cells recognise it as different and produce antigens to get rid of the pathogen. When the correct antigen is found and made they multiply and destroy the pathogen. The white blood cells also make a memory cells with instructions on how to make the antigen again if it is need again quickly.
If someone has been vaccinated against a pathogen and then the pathogen enters their body, the white blood cells remember the pathogen and use the memory cell to create the antigens that will destroy the pathogen.
Doctors vaccinate against many viruses such as measles, mumps and rubella ( the MMR vaccine), but they also vaccinate against bacterial infections, some of these are meningitis, diphtheria and tetnus.