In a nutshell, Fannie Mae and Freddie Mac are intermediaries. They buy loans from lenders and then turn around and sell them to investors.
Most mortgages, regardless of the type, are originated with the expectation that they will be sold after they close.
Loans that are kept in-house, by a big-box lender or perhaps a smaller bank, are called portfolio loans. These loans often carry higher rates and have more stringent guidelines than other mortgages, in that the owner of them is taking all of the responsibility and the risk of the borrowers defaulting on them.
When a loan that meets specific criteria closes, Fannie Mae or Freddie Mac buys it. The loan is then bundled with other similar loans and sold to investors as one security instrument.
The benefits to the lender are twofold.
First, the lender makes money on the mortgage, both in the origination of the loan, as in fees, and when the lender sells it to Fannie or Freddie. Second, when the lender sells the loan, it now has a new supply of money with which it can write new mortgages. Once a lender receives this new money, the cycle then repeats itself with the origination of more loans.
The benefit to investors is that they can buy thousands of loans that were originated to a specific set of guidelines and determine the level of risk that is suitable.
For example, they can specify whether a loan is for a fixed-rate or adjustable-rate and what equity a borrower must have.