Here’s the explanation I read about, probably in one of Richard Bach’s books.
US slang, from the WWII era (first printed record in the US Air Force in the 1950’s). Similar expressions like buy the plot and buy the lot also existed, although buy the farm is the only one to have survived. When a military pilot with a stricken airplane attempted to crash land in a farmer’s field, he would destroy a portion of the farmer’s crops for which the US government paid reimbursement to the farmer. If it were a bad crash-landing destroying most of the crops then the crash would cause the buying of the whole farm, shortened subsequently to the current idiom.
https://en.wiktionary.org/wiki/buy_the_farm#Etymology
That’s great. Thanks for sharing!