This dark joke comes from back in the day when if an airplane crashed on your farm you’d get a significant insurance payout from the federal government. So the joke became that the failed pilot “bought the farm.”
To them, it was a dark colloquialism from the world wars. Infantry would often talk to each other about how they were looking forward to "buying a farm" once the war was over and they went home to their families.
When the soldiers died in the trenches, as they often did, it was said that they had "bought the farm" as a euphemism. Usually it was better than saying how they actually died.
Not for the deceased, at least.