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."
A vaccine may also provide a non-active version of a toxin- a poison produced by a bacterium - So that the body can create a defense against it.
Once an antigen in noticed by the immune system, white blood cells called B-lymphocytes create a protein called an antibody that is 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 nay block the activity of the virus of bacterial stain directly, thus fighting in infection.
In addition, once in place, the antibodies make it much easier for other parts of the immune system to recognize and destroy the invading agent.