It's because vaccines use your own immune system against the infection. Your immune system is uniquely good at protecting at not just the exact disease but also possible mutations. Flu is (for the most part) the exception because it can do some unusual things.