"The environment determines what is "beneficial" and what isn't, for both humans and bacteria."
The environment isn't be beneficial, it just is.
organisms strive to stay alive and reproduce, aka continuation of the species, in whatever environment.
If the environment changes, slowly or quickly, slightly or dramatically, the organisms that adapt successfully (changes in gene expression, etc), respond successfully to environmental stressors (changes in energy sources, water, temperature, colors) reproduce and organisms that don't adapt gradually weaken and die.
I think there is too much prominence give to random events, like from DNA replication mistakes, or radiation for outer space.
eg, 10Ms of people have lactose or gluten intolerance because they have failed to adapt to the change in nutrition presented by farmed food like cow's milk and grains that became available only several 1000 years ago.