This answer will expand a bit on the theory. :)
A company, as an entity, represents a pile of value. Some of that is business value (the revenue stream from their products) and some of that is assets (real estate, manufacturing equipment, a patent portfolio, etc). One of those assets is cash.
If you own a share in the company, you own a share of all those assets, including the cash. In a theoretical sense, it doesn't really matter whether the company holds the cash instead of you. If the company adds an extra $1 billion to its assets, then people who buy and sell the company will think "hey, there's an extra $1 billion of cash in that company; I should be willing to pay $1 billion / shares outstanding
more per share to own it than I would otherwise." Granted, you may ultimately want to turn your ownership into cash, but you can do that by selling your shares to someone else.
From a practical standpoint, though, the company doesn't benefit from holding that cash for a long time. Cash doesn't do much except sit in bank accounts and earn pathetically small amounts of interest, and if you wanted pathetic amounts of interests from your cash you wouldn't be owning shares in a company, you'd have it in a bank account yourself. Really, the company should do something with their cash. Usually that means investing it in their own business, to grow and expand that business, or to enhance profitability. Sometimes they may also purchase other companies, if they think they can turn a profit from the purchase.
Sometimes there aren't a lot of good options for what to do with that money. In that case, the company should say, "I can't effectively use this money in a way which will grow my business. You should go and invest it yourself, in whatever sort of business you think makes sense." That's when they pay a dividend. You'll see that a lot of the really big global companies are the ones paying dividends - places like Coca-Cola or Exxon-Mobil or what-have-you. They just can't put all their cash to good use, even after their growth plans.
Many people who get dividends will invest them in the stock market again - possibly purchasing shares of the same company from someone else, or possibly purchasing shares of another company. It doesn't usually make a lot of sense for the company to invest in the stock market themselves, though. Investment expertise isn't really something most companies are known for, and because a company has multiple owners they may have differing investment needs and risk tolerance. For instance, if I had a bunch of money from the stock market I'd put it in some sort of growth stock because I'm twenty-something with a lot of savings and years to go before retirement. If I were close to retirement, though, I would want it in a more stable stock, or even in bonds. If I were retired I might even spend it directly. So the company should let all its owners choose, unless they have a good business reason not to.
Sometimes companies will do share buy-backs instead of dividends, which pays money to people selling the company stock. The remaining owners benefit by reducing the number of shares outstanding, so they own more of what's left. They should only do this if they think the stock is at a fair price, or below a fair price, for the company: otherwise the remaining owners are essentially giving away cash. (This actually happens distressingly often.) On the other hand, if the company's stock is depressed but it subsequently does better than the rest of the market, then it is a very good investment. The one nice thing about share buy-backs in general is that they don't have any immediate tax implications for the company's owners: they simply own a stock which is now more valuable, and can sell it (and pay taxes on that sale) whenever they choose.