This answer focuses primarily on allergies, for which there is more reliable research, but touches on general immunity towards the end.
It is certainly true that the prevalence of allergies is increasing worldwide, and seems to be related to a rise in the so-called 'modern lifestyle' which avoids exposure to potentially harmful environmental factors.
The prevalence of allergic diseases and asthma are increasing worldwide, particularly in low and middle income countries. Moreover, the complexity and severity of allergic diseases, including asthma, continue to increase especially in children and young adults, who are bearing the greatest burden of these trends. - World Allergy Organisation Journal
The idea that playing in the dirt builds an individual's immune system and reduces the chances of allergies etc. is known as the "hygiene hypothesis" (or, more accurately, the hygiene hypothesis states that
due to reduced exposure to microbial components, the proposed allergy-preventing potential of these factors is no more present in sufficient qualities and/or quantities, which leads to an imbalance of the immune system with a predisposition to the development of allergic disorders." - Epidemiological and immunological evidence for the hygiene hypothesis.
)
Aside from potential mechanisms, there is certainly evidence for the hygeine hypothesis
The researchers studied the immune system of mice lacking bacteria or any other microbes ("germ-free mice") and compared them to mice living in a normal environment with microbes.
They found that germ-free mice had exaggerated inflammation of the lungs and colon resembling asthma and colitis, respectively. This was caused by the hyperactivity of a unique class of T cells (immune cells) that had been previously linked to these disorders in both mice and humans.
most importantly, the researchers discovered that exposing the germ-free mice to microbes during their first weeks of life, but not when exposed later in adult life, led to a normalized immune system and prevention of diseases.
Moreover, the protection provided by early-life exposure to microbes was long-lasting, as predicted by the hygiene hypothesis. - Microbial Exposure During Early Life Has Persistent Effects on Natural Killer T Cell Function
As far as I know there is no consensus on this issue, however a number of scientific studies have proposed potential mechanisms to explain the rise in allergies and other such conditions; one of the most widely accepted ideas is that an avoidance of germs and microbes early on in life leads to
changes in the fine balancing of T helper cell 1 (Th1), Th2 and regulatory T cell (Treg) responses which are triggered by altered or missing innate immune cell activation. In fact, proper activation of cells of the innate immune system via their so-called pattern recognition receptors has been demonstrated to play a crucial role in early shaping of the immune system and suppression of the development of Th2-driven allergic immune responses. - Epidemiological and immunological evidence for the hygiene hypothesis.
this would seem to explain the observed phenomena.
It is definitely true though that there has been and will continue to be an element of natural selection involved here, as you have pointed out, because it is a force that is found pretty reliably in these sorts of situations. It is certainly logical that exposure to microbes during infancy will help weed out those with a weaker immune system (assuming they died because of infection/disease etc.), which over time would create a population and species that was more resilient. However, I can't find any studies to back up this idea. It is also a somewhat separate issue to the idea of the hygeine hypothesis, which describes changes to a specific individual within their lifetime rather than necessarily across a population due to natural selection and evolution, which is the explanation you have provided (unless I misunderstand your point?).
I only have a general understanding of immunity, however it is generally accepted that exposure to a pathogen (if this episode is survived) will result in an immune system which is better able to counter that pathogen in the future - this is the whole concept of vaccinations. therefore it makes sense that, again, low levels of contact with 'germs' will build a child's immunity.
Overall I think that it is a complex issue without a generally accepted answer as yet, however it seems to me based on this research that "playing in dirt" (in other words, being exposed to low levels of pathogens, microbes and toxins) does in fact build immunity and reduce the likelihood of developing allergies in later life, even if we don't know the exact mechanism for this yet.
aside from any potential immunity benefits, most children find playing in dirt fun (I know i did), so for the most part i'd say let them do it anyway!
My sources: