As other answers have pointed out, the mere fact that airlines still do it proves that it earns them money. But why would it make money even if most people don’t buy?
You have very good conditions inside an aeroplane for perfect marketing leaving the customer at a severe disadvantage. I’ll admit that they are getting worse, especially if more airlines and more destinations offer on-board Wi-Fi, but they are still there and the market is obviously growing.
There are a lot of people with little things to do. You present them with a complimentary magazine. A nonzero fraction will take it and look through it.
Inside this magazine, you can essentially advertise anything with any words you like. You can write duty-free everywhere (even if that specific product may not be eligible for duty-free at all; some products will be). You can print imaginary prices and then offer 50 % off (although many, especially European laws will prevent you from doing this if you land or are based in their country so this specific technique is not available everywhere). You can make things shiny and glittery and golden. A nonzero fraction of the first bullet point will want to buy something.
I’ll admit, some people don’t care about how much something costs. If it looks exclusive enough to them, they’ll bite the hook. Others may want to compare. ‘Darn, comparing prices in an aeroplane is really hard, the Wi-Fi is weak/not working/not offered and this is a type of item that I don’t usually buy. And it’s duty-free. And they’re offering another 10 % off. Damn, the price doesn’t look cheap, what does that actually cost in regular shops? It must be an expensive, exclusive item. I should go for it, it’ll never be as cheap again as now!’ A nonzero fraction of the former bullet point will choose to buy something.
This final number is undoubtedly a lot smaller than the number that would frequent a normal shop. However, many of the things in a normal shop just won’t work: come back later after comparing prices, the product will likely still be there. Thus, the margin of goods sold in-flight is going to be larger than that on-ground, and this difference in margin is why it is worthwhile even for a relatively small number of customers.
The above is only one set of marketing techniques proven to work — for all types of customers, indiscriminate of intelligence, social standing or other differences. Only the true absence of desire is a game-breaker (i.e. I’ll never buy cigarettes).
It’s by far not the only way how airlines can get a margin out of selling goods. As was pointed out in the comments (thanks!), some on-board prices are genuinely cheaper that off-board. The absence of taxes can, seemingly paradoxically, give a cheaper product a higher margin, as was also pointed out (thanks!).