Having a properly functioning immune system helps you to weather infections, but it cannot prevent you from getting sick from a pathogen if you do not have pre-existing immunity against that specific pathogen.
A fairly standard medical view.
If your body has the correct ph level and nutrition, germs will find it tough to gain a foothold and flourish in a bad environment. Vitamin D is part of a good terrain.
If your terrain is strong, your body will process germs and move them out quickly.
In other words, the other side of the coin you did not address is terrain of the body. Terrain is always s the first line of defense as part of your immune system. I refer you to the work of Antoine Béchamp.
Antoine Béchamp died over one hundred years ago; his work is hardly an example of cutting-edge research, and his hypotheses about the causes of infectious disease did not stand up to the test of time.
It is important to consume a balanced diet—nothing faddish, just maintain balance between the necessary macronutrients and consume sufficient micronutrients. But eating well and exercising can only go so far. You will still become ill if you are exposed to an infectious dose of a pathogen for which your body has no antibodies.
Pathogenic bacteria, viruses, and other microorganisms evolved to bypass our immune systems. Some pathogens are incredibly adept at not only evading our immune systems, but altering themselves to evade the active immune system, which is the component of immunity that kicks into action by producing antibodies when you are exposed to a pathogen.
If you are not in good health, then you become susceptible to illness from microorganisms that do not make healthy people sick. Also, as you age, your immune system declines and becomes less capable of fighting pathogens; this is why older people are so prone to infectious disease and why they must remain extra-vigilant about possible exposure.