Your immune system learns by exposure: the first time it meets a germ it improvises slowly, but afterward it keeps specialized memory cells that recognize that specific invader and respond in hours instead of days. A vaccine delivers a harmless version or piece of a pathogen — an inactivated virus, a protein, or genetic instructions to make one protein — so your body builds that memory without having to survive the real disease first.
This is why vaccine side effects (sore arm, mild fever) are usually the immune system working as designed, not the disease itself. And it's why some vaccines need boosters: memory can fade, and some pathogens mutate their outer proteins so last year's memory no longer matches — the reason flu shots are reformulated annually.