B and T cells are memory cells.

Your body has intrinsic, innate and humoral defense mechanisms, along with circulating neutralizing antibodies.

Intrinsic and innate defenses are early and quick and children and young people have strong defenses there and they can shred viruses quickly (and sometimes so fast that people don't form any humoral memory to the infection). If you ever get a '24 hour flu' where you feel like you have a degree of fever and then it passes that may have been an aborted infection that was destroyed by your innate and/or intrinsic systems.

Neutralizing antibodies are what most people think of as "immunity" which is immunity against infection. Small amounts of neutralizing antibodies will bind to foreign particles that are circulating and hinder them from being able to bind to cellular receptors and cause infection to begin with. This is true sterilizing immunity, and if its good enough it is a perfect shield. The neutralizing antibodies though bind to specific sites on the proteins of the virus (about 20 for the spike protein of SARS-CoV-2) and mutations at those sites can cause escape mutation variants (like Omicron). Neutralizing antibodies also tend to wane (probably because of autoimmune diseases that they could otherwise cause).

Once an infection has taken hold then you have intracellular virus and proteins. Those are chopped up by proteases inside the cell (in general proteins are often chopped up by the cell this goes on continuously) and those short protein segments are bound to a receptor called MHC-II which is then transported up to the cell surface and displayed on the surface of the cell. This is how cells signal what kinds of proteins they are "seeing" inside of them. There's a complicated system to distinguish between self-protein segments and foreign segments which I won't go into. Once the segments of the viral protein are displayed on the surface of the cell on MHC receptors that is a signal to other immune cells that there's something going wrong and the cell is either lysed and destroyed or dragged off to lymph nodes (by dendritic cells, which have nothing to do with nerve dendrites, but they looked kind of similar so got named that a long time ago).

Once in the lymph nodes the learning process happens and undifferentiated B-cell and T-cells are trained on the peptide and that leads to T-cells that can recognize the proteins. The fact that it is small slices of the protein and not the whole thing mean that there's actually many more segments that T-cells get trained on. There's a couple thousand T-cell epitopes in the virus, and they're overlapping and in many cases they're overlapping in conserved regions of the proteins where the virus likely cannot ever mutate without ceasing to be a virus. So T-cell antigenic escape will be impossible.

This process takes a lot of time though, and doesn't happen right away. There's also the process of hypermutation and affinity maturation which means that the whole humoral system looks a lot more like a bayesian spamfilter than a lock-and-key model. This is why giving a booster 6+ months after an initial shot(s) is effective at producing immune responses against variants that have mutations in the protein that the immune system is being trained on. You give the immune system "Viagra" as a string and it learns to identify "V14gr4" as well more or less.

Now the next time you get hit with the actual virus, even if its a variant, it may not be affected by neutralizing antibodies due to antigenic escape or waning immunity, but if you've got T-cells then they'll see those peptides being displayed by infected cells. Many or most of the peptides will be recognized as being exactly the same as the prior infection. Those T-cells will then either send out cries for help (CD4+ helper cells) and rally the troops, or else they'll directly lyse and destroy the infected cells (CD8+ cytotoxic lympocytes).

Because you've seen the virus before those T-cells have a huge headstart now and even though you don't have "immunity" in the sense that you've been infected, the severity is lower, because your body can identify the infected cells and immediately deal with them.

And one example of how we know that T-cells don't really wane and have considerable cross reactivity in the face of mutated proteins is the 2009 H1N1 pandemic. People that were born before 1957 were exposed to the H1 protein of the H1N1 strains that were circulating in humans from 1918 to 1957. That virus spent ~50 years mutating in pigs before hopping back into the human race, but the H1 protein in the 2009 H1N1 pandemic still was recognized by cross reactive T-cells in old people born before 1957 and that took the whole edge off of that pandemic. Everyone born before 1957 was effectively vaccinated (and probably boosted many many times against the 2009 pandemic). The H1 protein would have extremely mutated after 50 years of serial passage through probably billions of pigs, but conserved regions in the protein meant that the "spamfilter" was still trained on enough of it to give it a high score.

And that's the quick overview, there's a lot more.