On the first entry of a pathogen a division of B cells produce memory cells that remember the details about the pathogen to protect the body during it's next entry.
On the second entry of pathogen, the memory cells get activated and activate the remaining cells of immune system to kill it.
- This is basically a part of Secondary immune response which lasts longer than primary immune response( the response where the memory cell encounters the pathogen for the first time).
The process of activation of memory cells is still unknown and scientists are working on it.
The Secondary immune response makes a person immune to various diseases. That's why, a person surviving disease like Chicken pox, Small pox, Measles, etc. gets lifetime immunity to such diseases.
The concept of vaccinations are also based on the concept of "memory" of our immune system.