A vaccine is a preparation of a weakened or killed pathogen such as a bacterium or virus, or a portion of the pathogen's structure that upon administration to an individual, stimulates antibody production or cellular immunity against the pathogen.
The adaptive arm of the immune system produces memory cells that store memory of the pathogen e.g. virus that was used for the vaccine. When that same type of virus is encountered a second time, the memory cells remember it and trigger a very rapid, strong and well coordinated attack on the virus, effectively neutralizing and destroying it.