There are three stages in the life of a mortgage: origination, funding and servicing. The origination phase is where the borrower and loan professional sit down and determine the best course of action for the borrower.
Once this process is complete, and the borrower goes to the closing, the money is given to the borrower and/or seller, in what is called loan funding. Once the loan is funded, it needs to be serviced. This means collection of payments, escrows, and the disbursement of those escrows to the tax collector, insurance company, etc. This servicing can be done by either the lender itself, or contracted out to what it called a servicing company.
Regardless of who services the loan, the loan itself is either kept by the original lender, in what is called a portfolio, or sold to investors. Borrowers give lenders this right to sell their loans, through a loan servicing agreement. The original lender agrees that the terms of the loan will remain the same, regardless of who owns it, and loans may be sold multiple times.
When they are sold, mortgages of similar types and borrower grade, based on creditworthiness, are bundled together into what are called Mortgage Backed Securities, or MBSs. These securities are bought by and sold to investors around the world, similarly to other types of securities.
As with any other type of security, and as you might expect, they also carry risks to the investors that own them. Riskier and less creditworthy borrowers, many of whom are starting to default on loans that have been bundled, with like loans into these securities, are causing the securities themselves to lose value.