Vaccination involves the injection of small amounts of a pathogen - a microbe that causes disease - that is either dead or modified to provoke an immune response. Although these pathogens cannot cause disease, the body responds as if it was the real thing. The individual's white blood cells identify the antigens as non-self and begin a primary immune response, involving antibodies and phagocytosis to remove the pathogens.
After the injected pathogens have been destroyed, some white blood cells go on to become memory cells that exist in circulation for a very long time. These cells are primed to detect the antigen of the pathogen. If the person is infected with the same pathogen, but this time alive and active, they will quickly produce a secondary immune response to destroy the infecting pathogens and prevent illness.