I have recently read an interesting essay on this topic, but it was already explained it to me a few years ago.
The thesis would consist that, although the probability of winning classical lotteries is near zero (typically between 0.0000007% in the case of Euromillions and 0.000003% in the case of national lotteries), people is willing to pay an excessive overprice because they are buying the right to dream about the possibility of winning.
Although there are extreme cases that get addicted to lotteries, this is quite uncommon if I'm not wrong, because, if you are not paying for the probability but for the possibility, a bet of 1 USD is enough to buy said possibility.
On the other hand, national lotteries are known to be "taxes on ignorance of mathematics", but if these revenues financed public expenses that revert to the common good: would you agree to pay systematically 1 USD more in your annual taxes as something that ensures the right to dream of a dear life of every taxpayer?
It's true, the chances of ever winning the lottery are astonishingly small and lottery companies are increasingly making it harder to win by making little mathematical tweaks that make it magnitudes of difficulty harder. It annoys me when the "second place" prizes are also ridiculously small when they can be pretty similar in difficulty to win on as well. However, as you've stated, whenever I buy a lottery ticket it is to go to bed and dream of the tiniest possibility of waking up to complete financial freedom. The possibilities that winning millions would open up in your life are endless and there are very few similar shortcuts to wealth, so for a few dollars every so often it is nice to think of winning so big.