The numbers will tell you to invest the extra money instead of pay off early, assuming the interest rate is not crazy. Here is an example on mortgage on a $400,000 home with 20% down ($80,000) for a loan amount of $320,000.
The assumptions are shown in the picture below in BLUE. I did not include taxes and insurance in the monthly payment because you will be paying those no matter what. The S&P500 has returned historically returned just over 10% but I used a more conservative number of 8%.
In this example, a person can use the extra $183.08 to invest every month or to pay off the loan early and then invest the full $2,000 after the loan is paid off.
After 30 years, this person will have $72,269 extra money in the account if they invested the extra $183.08 right away. For better reference, the $72,269 would be $33,953 in todays dollar.
Psychologically, most people believe they are more secure if they pay off the loan faster. However, I disagree. In addition to having more money, someone could pull from the investment account if an unfortunate event happens (medical, lost job, etc.). The paying off early situation would not have money in the investment account until year 25. Once you pay extra money to the mortgage, that money is gone. Not technically, because you can pull from the equity but that is significantly more difficult.