The human immune system has quite an extraordinary ability to remember some of the pathogens it has encountered. When the body is first exposed to a particular virus or bacteria, the immune system starts a response to recognize and fight the foreign object. In the process, some B cells and T cells develop into memory cells. These cells then become a kind of biological record so that when the same pathogen or certain parts of it reappear, the body can provide a faster and stronger response.
Memory B cells can help the body produce antibodies when it encounters a recognized antigen again. Meanwhile, memory T cells can help recognize and respond to infected cells. But immune memory is not always perfect. Some pathogens can change so that the immune system has more difficulty recognizing them, and the strength of protection can also differ depending on the type of infection or vaccination. That's why a person can get the same disease more than once, even though the subsequent immune response can be different from the first infection. So, the immune system does not just have "memory", but has a network of cells that continue to adapt based on the body's experience of facing various threats.