Investment banks don't have to buy anything. If they don't think the stock is worth buying - they won't. If they think it is - others on the secondary market will probably think so too.
Initial public offering is offering to the public - i.e.: theoretically anyone can participate and purchase stocks. The major investment firms are not buying the stocks for themselves - but for their clients who are participating in this IPO. I, for example, receive email notifications from my brokerage firm each time there's another IPO that they have access to, and I can ask the brokerage to buy stocks from the IPO on my behalf. When that happens - they don't buy the stocks themselves and then sell to me. No, what happens is that I buy a stock, through them, and they charge me a commission for the service. Usually IPO participation commissions are higher than regular trading commissions.
Most of the time those who purchase stocks at IPO are institutional investors - i.e.: mutual funds, pension plans, investment banks for their managed accounts, etc. Retail investors would probably not participate in the IPO because of the costs, limited access (not all the brokerage firms have access to all the IPOs), and the uncertainty, and rather purchase the stocks later on a secondary market.