Meet Fannie and Freddie
Fannie Mae and Freddie Mac are huge financial institutions that buy mortgages and bundle them into securities that behave like bonds. Then they sell the mortgage-backed securities to investors.
Fannie and Freddie exist to keep money flowing through the mortgage finance system. When you get a mortgage, the lender sells the loan on the secondary market. (Often, the buyer is Fannie or Freddie.) By selling the mortgage, the lender gets its money back quickly so it can lend the money again, to another mortgage borrower.
The secondary market and you
Without a secondary market, mortgage lenders would be more reluctant to lend to you, because the lender's money would be tied up as you gradually repay it over the years. When the lender sells your mortgage, the lender gets the money back immediately, at a profit.
Meanwhile, investors buy these securities because they want stable payments for a long time.
It's these investors in the secondary market who collectively determine the interest rate of your mortgage loan. Your lender offers you an interest rate that investors on the secondary market are willing to buy.
Ups and downs of bonds
As with stock and bond markets, prices and yields on the secondary market move up and down. When the economy is on an upswing, investors demand higher yields on mortgage bonds, forcing lenders to raise mortgage rates. In a market downturn, interest rates tend to drop for consumers.
This blog post briefly explains how mortgage bonds respond to supply and demand.