What a market maker does is say "Sure, I'll take the other side of the trade - right now, at this price, even though I don't actually need or want the security. And I'll take on the risk that I might not be able to find a counterparty later. I trust my knowledge of the market enough to believe that the price I've offered you is one where I can profitably unload it later." And if they're wrong about gauging future supply & demand, they go bankrupt. Markets function on survivorship bias; repeat this cycle for long enough and the only market makers left are those that are extremely good at gauging future supply & demand and using it to set spot prices.
On some level, they're arbitraging risk & rationality. A retail investor makes a trade that is locally rational - perhaps they need to sell stocks to pay taxes, or purchase a home, or they've lost their job and need a savings cushion. And many of these events affect multiple people all at once (eg. everyone paying taxes on Apr 15, home sales peaking in the spring and declining in December, a recession causing people to lose their jobs all at once), which leads to temporary declines in demand that don't at all reflect the discounted value of all future cash flows of the stock market. Market makers smooth out these fluctuations, buying & selling stocks when it's globally rational based on the fundamentals of the companies and holding inventory when short-term supply does not match long-term supply.