How do vaccines work to protect against infectious diseases?

How do vaccines work to protect against infectious diseases?