When you are paying a mortgage with Z% APR interest, money given to the mortgage in 1 year is worth 1/(1+Z%) times as much. Ie, with 100% APR interest and a 100$ debt, paying 100$ today or 200$ in one year wipes out the debt.
You can translate all of your mortgage payments to "present value" using this technique: in effect, you owe 100,000$ today, and all of your future mortgage payments can be treated as time-deferred payments against that debt.
From this we can develop a mortgage equation. We start with:
A mortgage payment is a regular set of payments, each with a discount factor. If the per-time-period discount factor is x, a set of n payments equally spaced ends up being worth times as much as a single payment.
If we have 0.1% monthly interest and our payments are 750$ and our remaining debt is 100,000$, we get:
Using high school algebra algebra, we can solve for n. I'll skip the intermediate steps:
So 142 months, plus a smidgen. (This assumes there is a payment due immediately)
If we pay down principle, that simply changes one part of the equation.
Note that 0.999 is only an approximation of the discount factor if you are charged 0.1% interest per month. The correct value is (1-(1/(1.001)) = 0.999099909990999.
However, there will be variations based on local mortgage laws and what the interest numbers mean. In the end, the local mortgage laws and your contract describe how mortgage is calculated, when it is compounded, and what the rate advertised means.
Because of this, and the issues with rounding etc, the right way to do this is not with an equation. Instead, simulate.
You start with the current amount you owe.
Then you deposit the money, reducing that amount.
Then, you simulate the payment schedule, accumulating interest and making regular payments. This simulation will have a point where you owe nothing. That is the resulting term.
Equations like the above let you approach the problem analytically, in that I can calculate useful approximations like the derivative of mortgage length with respect to downpayment, or interest rates, or payment amount, or payment frequency, in a continuous manner.
In the end, a 30 year mortgage consists of a mere 360 payments. Asking a computer to do that simulation, where your interest can even vary by the number of days in each month, is easy; and calculating the result this way is as viable as using a fancy equation.