A vaccine contains an antigen from the pathogen. When injected into your body, your immune cells recognise this as foreign and produce antibodies against it. If you are then exposed to the real pathogen, your immune system has already produced antibodies to fight it off. You won't get sick because your immune system knows how to deal with the pathogen.