It's simpler and more accurate to say that the interest is added to the loan balance and then payments are subtracted. They don't subtract interest from payments, they add it to the balance. Extra payments don't have any interest applied to them, they go straight to principal.
The comparison though is between paying your extra cash flow towards a mortgage or investing it. You don't take money from the investment to pay the mortgage. You pay the minimum to the mortgage and then invest the difference.
As far as why banks want to do loans, you have to understand how banks work. What banks do is different.
Banks take money from deposits and then loan that out. They pay 1% interest on saving account deposits and charge 4% on loans. They are effectively purchasing something at $1 to sell for $4. That's a huge markup (400%). Of course banks want to do loans. They also have fractional reserve working in their favor. They can loan out more money than they actually have in deposits because the money is created out of thin air.
This is the same model used by savvy investors. They make investments using Other People's Money™.
https://docs.google.com/spreadsheets/d/12NGCBeup3rCygMsSbcOn...
As for the origin of mortgage money, you're forgetting about the secondary market. Lots of loans originated by banks get bundled up and sold off. That's a very lucrative business for some banks, because they can collect fees for the origination and get their money back right away with no risk.
Year 1 - Make a $1000 payment, lose $500 interest for a $9500 balance. Your investment yields $500 interest, but you had to withdraw $1000 to make the payment, so you're at $9500.
Year 2 - Make a $1000 payment, lose $475 interest for a $8975 balance. Your investment yields $475 interest, minus the $1000 withdrawal leaves you with $8975.
And so it goes, down to the end. Compound interest works both ways.
Remember what I said above: somebody thought that investing in your mortgage was the best return they could get. Are you willing to bet against them?