Vaccines help prepare the body to fight invasion by a specific pathogen by priming the body's immune system to recognize, react and fight against that specific pathogen should it invade the body.
The initial exposure of the body to an infectious agent e.g. through a vaccine, triggers an immune response most of which rapidly diminishes after the vaccination. However some cells of the immune system called lymphocytes remain with a memory of that pathogen.
As a result, if the same pathogen infects a vaccinated person, the memory cells remember and recognize it and rapidly spring into action triggering a much more rapid response specific to that pathogen than the initial exposure, effectively dealing and neutralizing the pathogen.