A vaccine contains small amounts of dead or inactive pathogens, which are put into the body. When they enter, the white blood cells recognise that they aren't good for the body by their antigen. Then, the white blood cell creates antibodies, which then attach to the antigen of the pathogen. This means the pathogen is now destroyed.
After that, the antibodies cause memory white blood cells to be made, which remember the antibody. This means if the pathogen enters again the antibodies are made much quicker and the pathogen gets destroyed by the phagocytes pathogen before the person gets ill.