This term was used during World War 2 whenever a Allied Pilot would have to make a crash landing into a European farm/house. WW2 pilots who did this were actually charged for the damages they caused and actually in a sense:
"bought the farm"

Billy: "So how's your uncle Jimmy doing?"
Bob: "Pretty good. You know that farm he was going to buy? The big one in Kansas with all those cows and shit, you know? Yeah, well he bought it."
Billy: "No way, you mean he bought the farm?"
Bob: "Yeah isn't that the bomb diggity?"
Billy: "You're sick, man. That's really fucking sad."

Buy the farm was originated with pilots. Mostly military.
If something from the government crashes on to a farm - like a military plane, the government 'buy the farm'. as in the land that it destroyed. The pilot is dead because of the impact...

Bob : You know Dave? He bought the farm whilst on a training excerise last week