Vaccines typically provide the immune system with harmless copies of an antigen: a portion of the surface of a bacterium or virus that the immune system recognizes as "foreign." (An antigen often plays a role in causing disease — for example by enabling a virus or bacterium to attach to cells.) A vaccine may also provide a non-active version of a toxin — a poison produced by a bacterium — so that the body can devise a defence against it.
Once an antigen is detected by the immune system, white blood cells called B-lymphocytes create a protein called an antibody that is precisely designed to attach to that antigen. Many copies of this antibody are produced. If a true infection of the same disease occurs, still more antibodies are created, and as they attach to their targets they may block the activity of the virus or bacterial strain directly, thus combating infection. In addition, once in place, the antibodies make it much easier for other components of the immune system (particularly phagocytes) to recognize and destroy the invading agent.
Immune systems are designed to "remember" — once exposed to a particular bacterium or virus, they retain immunity against it for years, decades, or even a lifetime — and so are prepared to defeat a later infection, and to do so quickly. This ability, and the speed with which it occurs, is a huge benefit: a body encountering a germ for the first time may need from seven to 12 days to mount an effective defence, and by then serious illness and even death may occur.