Everything checks out and Joe gets his loan. Yippee!
Local Bank issues lot of loans to borrowers like Joe. But at some point, Local Bank runs out of money to loan.
So Local Bank decides to clean up its balance sheet.
It chooses to sell those mortgages to a government-sponsored institution, like
(FRE), or to an investment bank.
The Federal National Mortgage Association, a.k.a. Fannie Mae, was formed by the by the government back in 1938 to promote home ownership in the America. Fannie would purchase the mortgages from the banks to free up the banks' money so they could originate more mortgages.
The Government National Mortgage Association, Ginnie Mae, and the Federal Home Loan Mortgage Corporation, Freddie Mac, were formed in 1968 and 1970, respectively. They play a similar role to Fannie Mae but target different segments of the mortgage market.
More recently, a number of Wall Street firms have gotten in on the act. A lot of the mortgages they are buying and securitizing are made to less creditworthy borrowers -- stuff Fannie and Freddie won't buy.
So Local Bank sells its mortgages to Fannie, Freddie or whomever, to free up its capital. Freddie, Fannie and the investment banks will now receive the principal and interest payments on those mortgages.