When homeowners pay their mortgage every month, the money goes to their local lender. In return, the local lender sells it to an investment bank. The investment bank combines thousands of mortgages and other loans including car loans, student loans and credit card debt to create complex derivatives called Collateralized Debt Obligations (CDOs). The Investment bank then sells the CDOs to investors.
When homeowners pay their mortgage, the money goes to investors all over the world. The investment banks pay rating agencies to evaluate the CDOs and many of them were given AAA rating, which is the highest possible investment grade. This made CDOs popular and attractive alongside retirement funds, which can only be purchased as highly rated securities.