Rain water and waste water can contain harmful chemicals if you drink it directly. Soil can filter some of those chemicals by adsorption, later on plants can filter even more.
But, not all soils and plants have the same filtering properties. For example, some plants can absorb heavy metals and arrest them in nodules in their roots, you wouldn’t want to eat that plant.
In general, unless you’re close to a contaminated area, the filtering provided by the soil and the plant is enough, meaning the plant is safe to eat. That is without considering the chemical and physical defenses plants make to fend off herbivores.