First, a firm decides to go public. Generally to raise capital, but sometimes for legal reasons (e.g. it has too many different private owners, which is illegal).
Once the firm has decided to go public, they hire a bank. The bank essentially does 4 things. The first is valuation. In other words, the bank decides how much the company is worth. This is an important calculation because it determines how to price the shares (company worth = number of shares multiplied by the price of a share).
The second thing the bank does is underwrite the shares. This means that the bank literally buys the portion of the company that is going to go public and then slice up that ownership into individual contracts (shares).
The third thing the bank does is take the shares of the IPO that it owns and goes on a "roadshow". The bank visits all of the big asset management firms (e.g. pension funds, hedge funds, traditional asset managers) and pitches the stock at the price they've determined. The bank will generally wind up selling all of the shares it plans to on the road show. The asset managers will become owners of the company once it starts trading...and they can choose to sell the shares they purchased on the first day of public trading. This is often very lucrative for the asset management firms who have the connections needed to have access to the IPO before it goes public.
The fourth thing the bank does is keep some of the shares for itself. The bank that IPOs the stock become a market maker for the stock. It needs to keep the stock liquid, so the IPO bank will trade its inventory of shares on the exchange and in off-exchange venues (like dark pools). It may also use the shares to structure other instruments like equity derivatives or ETFs. But, the main thing is that the IPO bank needs to be able to trade its inventory to keep the stock price stable. (This last part can be tricky...often, the stock pops a lot on the first day of trading and the IPO bank makes a lot of money. Other times, like the case with FB, Morgan Stanley had to throw a lot of money at the stock to keep the price stable for the first week of trading. Then the price fell considerably).
Also worth mentioning is that the bank will work with the lawyers on the legal side of things so that the client doesn't have to be as involved. But this is more of a support function than the primary activity of the bank during an IPO.