So this is an extremely simplified answer but essentially some pathogen gets into the body. If they find a cell first, they generally get to do their job and try to kill the cell and replicate. But if a macrophage (a type of white blood cell) finds them first betfore they've intruded upon a cell, the macrophage eats it and disassembles it into little tiny pieces.

The macrophage then hands over some of the important pieces to undifferentiated T cells. Those T cells then "differentiate" into one of two forms.

The first are "Helper T cells" which carry the "design" for antibodies (immunoglobulins, i.e. proteins that bind to pathogens directly. These then share those antibody designs with B-Cells.

The other type are "Killer T cells" which carry the "design" for T-cell-receptors that can detect "sick" cells for this specific pathogen or defect. They go hunt after the specific cell and essentially cause them to explode with the power of hydrogen peroxide. Then the macrophages eat up the dead infected cell and all the pathogens inside it and start the process anew.

Now those B-Cells get the "design" for antibodies from the helper T cells and differentiate into two types.

The first is essentially a factory that mass produces the antibodies and dumps them into the body. Those antibody proteins then bind to the pathogens and the macrophages can then directly attack the pathogens (because they have a bunch of big flags/alarms on them).

The other type of B-Cell that they can differentiate into are memory B-cells. These keep the designs stored inside them and keep detectors for the pathogen on their cell membrane. Then they "go to sleep" until their detectors are activated by the pathogen. They live out their lives and replicate as needed to continue their lineage. When a pathogen shows up, they bind to either the pathogen directly or to some of the proteins it produces and they turn their factories on at full speed and start mass producing antibodies to start the immune response as fast as possible locally before the pathogen can do damage. They get depleted in this effort of course but if things go okay, the following immune response should trigger the creation of more memory B cells. (and when they don't you get stuff like immune amnesia).

There are also "memory T-cells" but how they come about and how exactly they work is fuzzy and not super well understood. It's similar to memory B-cells but it's way more complicated and a bit "magic".

But yeah eventually then your body beats the infection and things go back to normal with the memory cells hanging out in the body.

Now the important thing with intensity of infection is that a mild infection will generally guarantee your body learns a sane, moderate response but a major infection can send your body into a panic and put the immune system into overdrive. That can train a response that attacks the pathogen but also attacks a lot of other stuff in the process (auto-immune response).

You can think of this kind of like an analog version of machine learning on proteins (the training input). A bad fit can end up mischaracterizing healthy cells and bodily structures as "pathogens" and cause long term issues or even just severe reactions when you get a reinfection.

Now for getting "trained" from the parent, this happens during pregnancy by diffusion of a subset of the antibodies from the mother to the fetus (not all types can but many do). Those stick around for a good while and eventually the child gets minor exposure to various pathogens and those shared antibodies kickstart the child's immune response enough to build up its own memory.

Hope this helps.

https://upload.wikimedia.org/wikipedia/commons/4/41/Primary_...