Coined by Heinlein in The Moon is a Harsh Mistress.
It's far older than that. At least a 150 years old.
Bars used to provide free lunches to encourage people to buy more drinks. Often very cheap and salty lunches, like stews or corned beef. This was very common in the 1920's-1940's, decades before Heinlein. It later evolved into just bowls of peanuts or pretzels on bars, though I haven't seen a bar with complimentary beer nuts in years.
The stew looked like a free lunch, but there's no free lunch because you paid for it in drinks.
I first heard it in person as advice from people who were born in the 1890's and I think it was something that had been explained to them as children themselves.
Seemed to me it probably originated in New York City.
Heinlein was referencing a familiar but cynical saying which is an example that was intended to be kept in mind whether you understood the full implications or not.
what?