There are many theories for the origin of farming. A classic explanation is that farming was simply a reaction to Malthusian pressures. Another, implied in Big Gods: How Religion Transformed Cooperation and Conflict, is that ideological factors may also have played a role in the emergence of sedentary lifestyles and so eventually farming.
I don’t have a strong opinion about the trigger for farming. What we know is that forms of farming seem to have emerged in very disparate locales after the last Ice Age. This is a curious contrast with the Eemian Interglacial 130 to 115 thousand years ago when to our knowledge farming did not emerge. Why didn’t farming become a common lifestyle then? One explanation is that behavioral modernity wasn’t a feature of our species, though at this point I think there’s a circularity in this to explain farming.
It seems plausible that biological and cultural factors over time made humans much more adaptable, protean, and innovative. We can leave it at that, and assume that the time was ripe by the Holocene.
Also, we need to be careful about assuming that modern hunter-gatherers, who occupy marginal lands, are representative of ancient hunter-gatherers. Ancient hunter-gatherers occupied the best and worst territory in terms of productivity. If territory is extremely rich in resources, such as the salmon fisheries of the Pacific Northwest, then a hunting and gathering lifestyle can coexist with dense sedentary lifestyles. But the fact is that in most cases hunting and gathering can support fewer humans per unit of land than agriculture.
The future belongs to the fecund, and if farming could support larger families, then the future would belong to farmers. Though I don’t think it was just a matter of fertility; I suspect farmer’s brought their numbers to bear when it comes to conflicts with hunter-gatherers.
Of course, farming is rather miserable. Why would anyone submit to this? One issue that I suspect needs to be considered is that when farming is initially applied to virgin land returns on labor are enormous. The early United States is a case of an agricultural society where yeoman farmers, what elsewhere would be called peasants, were large and robust. They gave rise to huge families, and never experienced famine. By the time the frontier closed in the late 19th century the American economy was already transitioning to industry, and the Malthusian trap was being avoided through gains in productivity and declining birthrates.
The very first generations of farmers would have experienced land surplus and been able to make recourse to extensive as opposed to intensive techniques. Their descendants would have to experience the immiseration on the Malthusian margin and recall the Golden Age of plenty in the past.
And obviously once a society transitioned to farming, there was no going back to a lower productivity lifestyle. Not only would starvation ensue, as there wouldn’t be sufficient game or wild grain to support the population, but farmers likely had lost many of the skills to harvest from the wild.
Finally, there is the question of whether farming or hunting and gathering is preferable in a pre-modern world. I believe it is definitely the latter. The ethnography and history that I have seen suggest that hunters and gatherers are coerced into settling down as farmers. It is never their ideal preference. This is a contrast with pastoralism, which hunting and gathering populations do shift to without coercion. The American frontier had many records of settlers “going native.” Hunting was the traditional pastime of European elites. Not the farming which supported their lavish lifestyles.
Many of the institutional features of “traditional” civilized life, from the tight control of kinship groups of domineering male figures, to the transformation of religion into a tool for mass mobilization, emerged I believe as cultural adaptations and instruments to deal with the stress of constraining individuals to the farming lifestyle. Now that we’re not all peasants we’re seeing the dimishment of the power of these ancient institutions.