With viruses and bacteria the immune system eventually catches up and vaccinating tends to be done before the host is infected (rabies vaccination is an outlier since it still works even after infection during the latent period). With cancer the immune system usually doesn’t recognize the threat which is why vaccinating after the fact is still effective.
The most common application is as a preventative measure to protect you from an infectious disease. But it can be used for other diseases (like cancer, which is not infectious) and it can be used after you're sick.
Fun fact: if an unvaccinated person is exposed to measles, they can get the MMR within 72 hours of exposure, and it will still reduce the severity of the disease. (Source: https://www.cdc.gov/vaccines/vpd/mmr/public/index.html )