I think the earliest instances of at least the term itself, refer to generating in game items and selling them to other players for real currency, for example 'gold farming'.
There might be earlier less well known instances, but the first one that made the news outside of the game it was in was, most likely Ultima Online (1997). Wired Magazine wrote a nice retrospective on the subject, also mentioned in this article.
Other early games where this became a feature (whether wanted or not) were Everquest (1999), RuneScape (2001) and of course World of Warcraft (2004).
Later on the term expanded to include repetitive tasks done by the players themselves in order to gain resources (as opposed to grinding, which is usually about gaining XP or advancing levels faster).
The activity itself of course predates online games as described above (early RTS games like Dune 2 (1992) for example literally made you deploy a harvester to collect spice, and more spice meant a bigger/better army), but I don't recall these being referred to as such.