The ecological and evolutionary dynamics of populations are shaped by the strategies they use to produce and use resources. However, our understanding of the interplay between the genetic, behavioral, and environmental factors driving these strategies is limited. Here, we report on a Caenorhabditis elegans–Escherichia coli (worm–bacteria) experimental system in which the worm-foraging behavior leads to a redistribution of the bacterial food source, resulting in a growth advantage for both organisms, similar to that achieved via farming. We show experimentally and theoretically that the increased resource growth represents a public good that can benefit all other consumers, regardless of whether or not they are producers. Mutant worms that cannot farm bacteria benefit from farming by other worms in direct proportion to the fraction of farmers in the worm population. The farming behavior can therefore be exploited if it is associated with either energetic or survival costs. However, when the individuals compete for resources with their own type, these costs can result in an increased population density. Altogether, our findings reveal a previously unrecognized mechanism of public good production resulting from the foraging behavior of C. elegans, which has important population-level consequences. This powerful system may provide broad insight into exploration–exploitation tradeoffs, the resultant ecoevolutionary dynamics, and the underlying genetic and neurobehavioral driving forces of multispecies interactions.