I assume the relatively new versions of windows calculate the battery remaining the same. If not, to avoid broadness, show the calculation for windows 8/8.1.
The short answer is: it is complicated. A lot of factors go into the formula for "Time left calculation". The only effective thing that can be measured for a battery is its voltage, which can indicate its remaining charge, should the perfect curve is available. That is for remaining energy, yet, you need the rate at which energy is being consumed and that highly depends on system load. A lot of calculation is being made in between.
PS. The team at MSFT who works on this very problem is called Windows Fundamentals team.