How we compare
Prepay: the extra cash goes to principal. Once the loan is paid off, the whole freed-up payment plus the extra is invested every month until the original payoff date. Invest: the extra cash goes into investments every month and the mortgage runs its normal course. We then compare the investment balance after tax on gains at the original payoff date, when both paths have no mortgage.
Worked example
$300,000 at 6.5% with 25 years left and $500 a month extra. Prepaying clears the loan in about 15 years 11 months. With a 7% return and 15% tax on gains, prepaying ends at about $367,549 and investing at about $366,780, almost a tie. With no tax (for example, in a Roth IRA), investing comes out ahead, about $405,036 vs $383,778.
The rule of thumb: prepaying earns a guaranteed return equal to your mortgage rate. Investing wins only if your after-tax return beats that rate, and it comes with risk.