Traditionally, the reason for going public was to raise money to expand the company, figuring that a smaller percentage of a company that was much more profitable would produce more income for the original owner(s), more quickly, than trying to grow without that assistance. Same reason one might accept a venture capitalist as a partner earlier in the process; which also involves giving up part of your ownership.
But that was back when the market was mostly about investing, and the primary way owners (including stockholders) took profit out of a company was through dividends, as a percentage of the company's income. (Sometimes referred to as "clipping coupons".) Alternatively, folks could do a buyout, or accept partnership as a partial buyout, and take a larger chunk of cash immediately while giving up part of their future income.
When everyone and her cousins jumped into the market a few decades ago, the focus shifted from owning and dividends to trading. Folks started demanding that companies grow as fast as possible so their holdings increased in value rapidly, and that lead to an environment where more of the profit is fed back into the company and dividends are smaller. In this environment, the way folks take most of their profit out of a company is by selling shares, and that can apply to the owners too. If your million dollar company has grown 10%, you can sell $100k worth of stock and still own a million dollars worth of the company. And these days even a small mom-and-pop operation may be worth $1M, so you're usually talking about much larger numbers. Selling stock now can raise the kinds of money that used to require a buyout or partnership, and can be done incrementally in smaller units as well as large transactions. And a thousand small stockholders don't meddle in the company's day to day management as much as one major investor will want to.
So, yes, it's a mechanism for turning part of the company's paper wealth into cash. For an IPO, how much goes to the owners/stockholders (as increased dividends, increased executive wages, whatever) and how much is used to grow the company is a case by case decision by the company's board.
Of course once the stock is on the public market, trading doesn't affect the company's operation... but again, if the stock increases in value that makes the company's retained ownership worth more if they decide to sell more stock, which also makes it possible for them to borrow more or borrow at better rates, so indirectly an increased stock price does still benefit the company. Which is where the pressure to raise stock value rather than issue dividends feeds back into their decisions.