When a virus is transmitted through the air, wearing masks can significantly reduce the likelihood of transmission. An effective mask should be snug around your face (no gaps) and should be made of a material woven tightly enough to filter out the smallest of viral particles. Masks containing multiple layers provide more protection than a single layer mask, as the additional layers are able to filter more particulates. Masks help prevent you from breathing viral particles in as well as aiding in preventing you from breathing out viral particles and spreading them to others.
There are many different kinds of vaccines. Some inject a weak or inactive virus into your body so that your immune cells become familiar with it and have an army at the ready should it encounter it again, some inject specific proteins of the virus, and some use mRNA from the virus that codes for harmless proteins that can help your body recognize the virus. (there are many more types of vaccines but these are the most relevant)
Vaccines use messenger RNA) inside a lipid membrane. This fatty cover both protects the mRNA when it first enters the body, and also helps it to get inside cells by fusing with the cell membrane. Once the mRNA is inside the cell, the ribosomes inside the cell gets to work and translates it into the antigen protein (in this case, the spike protein). This mRNA typically lasts only a few days, but in that time sufficient antigen is made to stimulate an immune response. It is then naturally broken down and can no longer be read by the ribosomes. Once the body has come into contact with these manufactured spike proteins, it builds up an army ready to fight these spikes should they ever again be encountered. If Covid-19 does enter the body, the programmed immune cells will attack and destroy the spike proteins. This prevents the virus from entering and replicating inside the cells. Currently this vaccine is delivered in two doses with maximum immunity being reached approximately 2 weeks after the final dose.
*There are many types of immune cells involved in this process but this gives you super basic insight into some of the major players. If you take AP bio or Genetics you will learn a lot more about the immune system and how it works.