Commercial air travel is not, by itself, a pleasant experience. You have to take a cab or bus to the airport, wait in line, pass security, wait in line, check in, wait for your flight, sometimes delayed, wait in line, board, wait in line to sit down. Then, spend hours sitting with little to do, especially if you're in economy without IFE.
Eating is an inherently pleasant activity, since it does play some role in basic survival. Hunger is being satisfied, hormones are being produced. If nothing else, it's at least an activity that breaks up the experience of just sitting.
Mid-century long-distance airlines took inspiration from ocean liners, which definitely do serve food. The Stratocruiser, the Tristar, and early 747s had spacious dining lounges, on the lower deck (now taken by cargo) or the upper deck in the Queen, plus bars on the main deck.
Does this image inspire you to fly more than that of a row of seats? If you're a non-drinker, feel frree to pick another from the site above.
The reason in-flight food hasn't been entirely phased out is that, even served on a seat-back tray table, it gives some enjoyment to most passengers. That leaves the flight as a more pleasant experience in memory, and makes them less unlikely to pick air travel for their future trips.
If it's an actual hot meal, which are becoming increasingly rare, it might be one's only quantum of solace through the whole ordeal. A mix of good and bad leaves a better aftertaste than just the bad.
Cost-wise, it's not a big expense. You can fit a few extra seats if you completely remove the galley, but you run into the exit limit. Even Ryanair only halved it in size, since they sell what little fits at a massive markup. The food itself is pennies compared to fuel and airport fees, the two biggest expenses in air travel. Even full-service airlines spend $3 to $6.50 on an economy meal, and as little as $1 for low-cost carriers. This figure is going down year by year. Airport fees are going up, reaching $35 and even $50, which makes business tougher for the airlines.
They don't serve food on long haul buses and trains.
They definitely do - long-distance trains have dining cars. Some are still as luxurious as in the 1800s. Long-haul buses make stops at roadside cafes - sometimes too many stops, but you can count on at least one.
On a personal note, separate dining cars remain one of the reasons I prefer train when traveling as a passenger, as long as I can afford the extra time. It's not just about the food, but also about more social opportunities, and a more dignified feeling overall.
Of course, train economics are different - they can be almost arbitrarily long, drag is almost fixed, rolling resistance is low, electrics also use regenerative braking, so added weight or even an extra car is cheap.
In the air, it's not. Lounges in the air cost their weight in fuel to carry. They're not completely gone - you still get them on some better carriers, most reliably on Emirates A380 flights.
But in-seat airline meals, excluding water, only take up ~100 kg for a narrowbody (320 or 737), or 0.25% of total weight, making them a small concession to comfort.