People are immunised against pathogens through vaccination - usually an injection.
A vaccine is comprised of dead, inactive or weakened pathogen or virus. These act as antigens (foreign proteins) that the body recognises and stimulates the white blood cells (lymphocytes) of the immune system to produce an antibody response.
When the body first encounters a new antigen it takes time to produce lots of antibodies to fight off the pathogen. When someone is immunised it gives the body time to produce the antibdoes but does not cause disease/illness as the vaccine does not contain live or active pathogens. The immune system remembers what the antigen looks like and the antibody needed to fight it so if the body encounters the same antigen again, it can produce a much quicker immune response producing lots of specific antibodies.
Vaccines are only specific to the pathogen given becuase the antibodies produced in respone are specific for their shape.
Vaccines are widesly used to reduce spread of infection amongst a population (less
likely to get an epidemic). If enough people are vaccinated to stop pathogens infecting whole populations it is called herd immunity.