Immunity from viruses depends on the virus, including how it’s shaped, how it infects us, how often it mutates, and so on.
Our immune system cells recognise viruses in lots of ways, for example by the proteins on their coats, or the chemicals they produce.
For a large, stable virus like measles, with a relatively consistent protein capsule, it can only withstand a certain degree of mutation before it collapses. So the virus tends to stay looking very much the same over time, and our immune systems can recognise and respond to measles virus coat proteins a long time after either infection or vaccination.
Other viruses like norovirus are smaller and though they infect our cells easily, our immune system response to them is partly affected by our own genotype, for example our blood type.
Yet more different viruses like flu or Covid are small, rapidly mutating viruses which mutate often and often change the protein chain arrangements on their coats or surfaces, and thus can evade our existing antibodies and immune system cells more easily. This is why some viruses or vaccinations don’t give lasting immunity whereas others (like measles) do.
We have a variety of immune system cells in our bodies, including different types of white blood cells, T-cells, B-cells and so on. Different combinations of these are active against different viruses. Some of our immune cells literally “eat” or engulf particular viruses. Others produce chemicals that burst a virus’s coat or capsid. Some kill viruses in yet other ways. Some viruses are able to “hide” from the immune cells in particular nerves or organs that protect them better from being “seen” by our immune cells.
There are lots of ways that our immune systems and different viruses interact — so much so that it is difficult to give any one overall picture of how it works; and we are discovering more all the time.