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?
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.
Infinite Banking (using overfunded, dividend paying, whole life insurance) currently has 4.5% guaranteed floors with actual rates with dividends currently at 5.0 - 5.5%. Lots of other benefits as well, life insurance, and being able to borrow money against your policy any time you want for any reason, no questions asked.
Also, a 4% mortgage is only 4% of the whole balance in the beginning. At the end it is 4% of a very small balance. If you use level payments you effectively cut the interest rate in half to make comparison.
Taking out a $10,000 loan at 5% and investing it at 5% results in a profit.
Year 1 - Make a $1000 payment, $9000 balance (5% is $450). Your investment is $10000 at 5% or $500. $9000 loan and $10,500 in investments.
Year 2 - Make a $1000 payment, $8450 balance (5% is $423). Your investment is $10,500 at 5% $525. $8450 loan and $11,025 in investments.
...