Over the long term, stocks have historically beaten bond returns, even after accounting for the periodic market crashes. The general consensus is that your portfolio should at least be 25% in bonds. If I don't need this money for decades, meaning I can ride out periodical market crashes, why would I invest in bonds instead of funds that track broad stock market indexes?
If I don't need this money for decades, meaning I can ride out periodical market crashes, why would I invest in bonds instead of funds that track broad stock market indexes?
You wouldn't. But you can never be 100% sure that you really won't need the money for decades. Also, even if you don't need it for decades, you can never be 100% certain that the market will not be way down at the time, decades in the future, when you do need the money. The amount of your portfolio you allocate to bonds (relative to stocks) can be seen as a measure of your desire to guard against that uncertainty.
I don't think it's accurate to say that "the general consensus is that your portfolio should at least be 25% in bonds". For a young investor with high risk tolerance, many would recommend less than that. For instance, this page from T. Rowe Price suggests no more than 10% bonds for those in their 20s or 30s.
Basically you would put money into bonds rather than stocks to reduce the volatility of your portfolio. If you care only about maximizing return and don't care about volatility, then you don't have to invest in bonds. But you probably actually do care about volatility, even if you don't think you do. You might not care enough to put 25% in bonds, but you might care enough to put 10% in bonds.
Bonds provide protections against stock market crashes, diversity and returns as the other posters have said but the primary reason to invest in bonds is to receive relatively guaranteed income. By that I mean you receive regular payments as long as the debtor doesn't go bankrupt and stop paying. Even when this happens, bondholders are the first in line to get paid from the sale of the business's assets. This also makes them less risky. Stocks don't guarantee income and shareholders are last in line to get paid. When a stock goes to zero, you lose everything, where as a bondholder will get some face value redemption to the notes issue price and still keep all the previous income payments. In addition, you can use your bond income to buy more shares of stock and increase your gains there.
I can think of a few reasons for this. First, bonds are not as correlated with the stock market so having some in your portfolio will reduce volatility by a bit. This is nice because it makes you panic less about the value changes in your portfolio when the stock market is acting up, and I'm sure that fund managers would rather you make less money consistently then more money in a more volatile way. Secondly, you never know when you might need that money, and since stock market crashes tend to be correlated with people losing their jobs, it would be really unfortunate to have to sell off stocks when they are under-priced due to market shenanigans. The bond portion of your portfolio would be more likely to be stable and easier to sell to help you get through a rough patch.
I have some investment money I don't plan to touch for 20 years and I have the bond portion set to 5-10% since I might as well go for a "high growth" position, but if you're more conservative, and might make withdrawals, it's better to have more in bonds... I definitely will switch over more into bonds when I get ready to retire-- I'd rather have slow consistent payments for my retirement than lose a lot in an unexpected crash at a bad time!
Many folks use bonds to diversify their portfolio since bonds rise and fall in value at different times and for different reasons than stocks.
Bonds pay interest on a regular basis (usually monthly or quarterly) and so some people invest in bonds in order to match the interest payments to some regular expense they might have. The interest payment does not change (fixed income).
For individual bonds, there is a maturity date at which you can expect to receive the face value of the bond (the issuer's creditworthiness is important here). You can make a little money on a bond by buying it when its value is lower than its face value and either selling later for a higher value, or waiting for it to mature.
Often the minimum investment for a single bond is high, so if you don't have a large enough amount, you can still get the performance of bonds through a bond fund. These do not mature, so you don't have a guarantee of a return of your investment. However, they have access to more bonds than retail investors, so the funds can keep your money more fully invested. If you don't need the income, you can reinvest the dividends and have a little extra capital growth this way.