How does getting vaccinated protect you and others from infectious diseases?

How does getting vaccinated protect you and others from infectious diseases?