For an amortized loan, you can calculate the monthly payment for a loan.
For example, 30 year loan at 7% for $100,000 would have a monthly payment of $665.30.
For the duration of your loan, your payment will be $655.30 no matter how much you prepay.
For each payment, you calculate interest owed (7% / 12 * Loan Balance). For the first month, you get 0.07/12*100,000 = 583.33.
For your first payment of $665.30, you'll pay $583.33 and $81.97 to principal.
This will reduce the mortgage balance to 99,918.03 for the next month. Month 2's interest payment will be 0.07/12*99,918.03 or $582.86 interest and $82.45 principal.
If instead, you paid $10,000 extra, your loan balance would be 89,918.03 and your interest would be 0.07/12*89,918.03 or $524.52 interest and $140.78 principal.
To determine how many payments are left, you calculate out how the principal reduces when keeping the payment constant and see when the loan is paid off.
Here's a reasonable calculator that lets you put in several one time payments or consistent repeatable payments: http://mortgage-x.com/calculators/extra_payments.asp
As an aside, the cost to pay a "month" of principal extra at the beginning of a mortgage is very small. At the beginning of the loan above you are paying $81.97 per month in principal. If you send an extra $82 to your mortgage in the first month, you reduce your mortgage duration by 1 month. $82 in month 1 means you save $665.30 during month 360. $165 in month 1 will save you $1330.60 during months 359 & 360.