Vaccines work by introducing an attenuated strain of the pathogen (or alternatively the antigens that are normally present on the pathogens surface) into the body, whereupon the body mounts an immune response. As this will (hopefully) be the first time that the body has encountered the antigens on the pseudo-pathogen's surface, the response is called the primary response.
This consists of two main divisions: the cell mediated pathway and the humoral pathway. In vaccination it is the humoral pathway that is important. This is where a division of white blood cells (B-Cells) produce antibodies that are complementary to the antigens on the pathogen surface, causing a negative effect to the pathogen (death, inability to reproduce, de-activation of toxins, etc.). However as each B-Cell produces a different antibody, there needs to be a mechanism to select the correct one:
- Antigen Presentation (technically part of the cell-mediated-response) - a phagocyte engulfs the pathogen and displays the pathogenic antigens on its own surface.
- Clonal Selection - B-Cells that are attracted to the invasion site attempt to bind their antibodies onto the pathogen's antigens. It takes time for this to occur successfully as you are essentially waiting for the correct mutation to happen.
- Clonal Expansion - Once a complementary antibody producing B-Cell has been found it is then activated with the help of a T-Helper cell. This causes it to divide rapidly whereupon these cloned specific B cells can secrete their antibodies which will cause detriment to the pathogen.
It's at this point that I can start to answer your specific question. The large clone of B cells will then sub-divide into two types. Plasma Cells remain in the blood and produce antibodies to fight the infection. The other type, much smaller in proportion, are called Memory Cells. These cells have a very long lifetime and move to lymph nodes across the body (including the spleen), where they remain dormant until the same pathogen is found again.
When this is the case, the memory cells are activated by T-helpers so that they can divide into massive numbers of plasma cells to fight the infection the second time. This secondary response is a much faster as the clonal selection stage does not have to wait for the right mutation - they are already waiting in the lymph nodes. The response is also much stronger as each memory cell can produce large numbers of plasma cells - i.e. you can start with multiple activated B-cells (as many as you have memory cells) rather than just the one that has mutated into a complementary shape in the primary immune response.
The aim of the vaccine is for the secondary response to be so quick that potentially life threatening symptoms do not occur; the body has time to find and store the correct antigen in a safe environment as the pathogen has been deactivated.