The goal of a vaccine is to give the immune system a head start in fighting an infectious organism so that the infection is prevented. There are many ways to develop a vaccine but they all involve using the whole organism or part of the organism to allow the immune system to develop a protective immune response.
Then, if the person is exposed to the organism (it could be a bacterium, virus, fungus or parasite), the immune system rapidly eliminates the organism so that it cannot produce disease. Because an immune response has been developed from the vaccine, the immune system can act very quickly if exposed to the organism.