When a doctor gives a patient a vaccine they are injecting them with a weakened or inactive form of the disease causing pathogen. This isn't strong enough to make the person ill but still triggers a response from the immune system.
When a vaccine is given, it allows the white blood cells to produce the antibodies needed to fight the disease. The memory cells within the white blood cells then remember the instructions to make that specific type of antibody again.
After being vaccinated, if the patient is exposed to the pathogen again the white blood cells are able to make the antibodies quickly due to the memory cells remembering them. This means that the body can fight the pathogen before it has the chance to make the patient ill.
An example of a bacterial disease that is vaccinated against is tuberculosis and a viral disease that is vaccinated against is measles.