Immunity from a infectious disease either comes from acquiring the disease and surviving, or from vaccination.
Some infectious diseases are "contagious" meaning acquired from another person,
while some are simply "infectious" and acquired from the environment.
"Herd Immunity" helps prevent the spread of Contagious diseases.
If a significant percentage of the population (the "herd") are immune, they cannot catch the disease
and so . . . those who are not immune, have no one to catch it from.
What constitues a "significant" percentage varies from disease to disease;
some diseases transmit from person to person more easily than others.
For example, herd immunity for measles requires 92% of the herd to be immune, influenza requires about 40%, while COVID-19 requires about 60-75%.
In the graphic above, one of the red figures is surrounded by blue figures, and that red figure can't catch the disease.
The other red figure is at edge of the herd, and can catch the disease from others outside the herd.
However, this red figure is surrounded by blue (immune) figures, so couldn't infect the rest of the herd.
Herd Immunity was likely acquired eventually during the Influenza pandemic of 1918/19.
Almost everyone "caught the flu". 20 to 50 million people died, those that didn't became immune.
In order to avoid such massive loss of life again, vaccines are now used to create herd immunity.
It is vital that everyone is vaccinated and not rely on the protection of the Herd, or the necessary percentage for immunity soon drops.