The body's immune system defends against invading organisms and other outside material that can infect the body. One part of the immune system is called the humoral immune system, which is specifically for defending against infectious invaders that are in the blood or other body fluids. The main cells that make it work are the B cells, which create and secrete antibodies that seek out the invaders and attach to them. Once the invader is coated in antibodies, this targets the invader for destruction by other parts of the immune system.
Before the B cell can create antibodies, it first needs T helper cells" to 'teach' the B cell about details of the incoming invader. That way the B cell can then create antibodies that specifically target only that invading organism and do not accidentally target parts of your own body for destruction as well. Of course this whole process takes time, about a day or two, so the humoral immune system does not create a quick defense against new invaders you have never encountered before. However, if your B cells have dealt with this specific invader before in the past, then the antibodies against that invader is already present in your blood, or the B cells that have fought this invader in the past can quickly create new antibodies against it again. So if your B cells have fought this invader in the past, then it can often fight off that invader again but quickly this time, so that the re-invader is killed quickly before it can start setting up house in your body again. This is why, in some cases, after a human has been sick with a specific illness and then fought it off, they can not get sick with it again. Your B cells and the antibodies they produce make you immune to that specific invader infection.