The human body meets thousands of microorganisms every day and defeats almost all of them without you noticing. Vaccination works by using that existing system rather than replacing it.
Two layers of defence
The innate system responds immediately to anything that looks foreign: skin and mucus as barriers, phagocytes that engulf invaders, inflammation that brings blood and immune cells to a site. It is fast and general, and it does not remember.
The adaptive system is slow and specific. B lymphocytes produce antibodies — proteins shaped to bind one particular molecule, or antigen, on a pathogen's surface. T lymphocytes destroy the body's own infected cells and coordinate the response. The first time a pathogen appears, finding and multiplying the few lymphocytes that happen to match it takes one to two weeks. In that time you get ill.
The point of memory
After the infection clears, most of those lymphocytes die, but a population of memory cells remains. On a second exposure the response begins within hours instead of weeks and produces far more antibody. The pathogen is cleared before it can cause symptoms. You were exposed; you were not ill.
A vaccine creates that memory without the first illness. It presents the immune system with a harmless version of the antigen: an inactivated or weakened pathogen, a purified surface protein, a harmless carrier virus, or mRNA instructions that let your own cells briefly manufacture the antigen themselves. In every case the pathogen's ability to cause disease is absent, and the recognisable shape is present.
Mild soreness or a brief fever afterwards is the innate system responding — a sign the process has started, not a sign of infection.
Protecting people who cannot be vaccinated
Every population includes people who cannot be vaccinated: newborns, some patients receiving cancer treatment, people with particular immune conditions. When a high enough proportion of the community is immune, chains of transmission break down and the pathogen struggles to reach those individuals. This is herd immunity, and the threshold depends on how contagious the disease is — around 95 per cent for measles, lower for less transmissible diseases.
That is why vaccination is usually described as a public health measure rather than only a personal one. The arithmetic of transmission does not respect individual boundaries.