When you encounter something new, your sensory organs (eyes, skin, nose, ears, tongue) send information to the brain. Nerve cells in the brain communicate with each other using electrical signals to send information accross a gap between cells called a synapse. Each brain cell can form thousands of these synapses, meaning that the whole brain can have 100 trillion synapses, and they can change all the time as we think and store memories. They can also be strengthened by repeated signals being sent accross the synapse. That’s why you need to read over your revision notes more than once to remember them! The part of the brain that is mainly responsible for memory is the hippocampus.
I have a terrible memory! Especially with peoples names. Joanne’s answer is great but you also have immune memory as well. When you get infected by a pathogen ( like a bacteria, virus or parasite) that is going to make you ill, your body creates special white blood cells called T and B cells which produce antibodies that specifically target the bug. Making antibodies takes a while to do but these white blood cells remember what the bug looks like so that when they meet the bug for a second time, they can produce the right antibody quickly and destroy it before you get sick again.
This is the reason why vaccination works as vaccines introduce the bug to your body in a safe manner and allow it to generate this immune memory. That way when your body meets the bug for real it knows how best to deal with it.