What is the mechanism behind vaccines protecting against illnesses?

What is the mechanism behind vaccines protecting against illnesses?