Competition between species is a major ecological force that can drive evolution. Here, we test the effect of this force on the evolution of cooperation within a species. We use sucrose metabolism of budding yeast, Saccharomyces cerevisiae, as a model cooperative system that is subject to social parasitism by cheater strategies. We find that when cocultured with a bacterial competitor, Escherichia coli, the frequency of cooperator phenotypes in yeast populations increases dramatically as compared with isolated yeast populations. Bacterial competition stabilizes cooperation within yeast by limiting the yeast population density and also by depleting the public goods produced by cooperating yeast cells. Both of these changes induced by bacterial competition increase the cooperator frequency because cooperator yeast cells have a small preferential access to the public goods they produce; this preferential access becomes more important when the public good is scarce. Our results indicate that a thorough understanding of species interactions is crucial for explaining the maintenance and evolution of cooperation in nature.