At least here in Germany, half of sale proceedings of state-licensed lotteries go into social causes (our open source ecology germany e.v. managed to get several projects funded through this). Also, they are obliged to check each player against a black list of gambling addicts, which unlicensed online gambling venues probably don't give a toss about.
It's not that the money from the lottery ends up in a slush fund, it's that the government cuts funding for education by the amount that it gets from the lottery. The money from the cuts gives the government the same amount of "clean" money to do whatever they want with.
Money is fungible and exactly this trick is common. Best to think of the education funding as a minimum below which funding cannot be cut. In Florida, where I live, it actually funds a specific scholarship program, so it really does have the effect of making the program difficult to slash.