How is the length of a leg calculated?
Many of the 'variables' you mention, are in fact fixed, and in large determined by dispatch (the smart folks at the company). For commercial flight, the flight plan will be filed in advance (generally by the company dispatch), and will contain all the waypoints en route, including expected flight levels. Dispatch makes the calculation for the most efficient flight in close collaboration with ATC authorities, for example by reserving slots on certain airways. 

Deviations from the flight plan are not common - you won't hear ATC tell an airplane mid-flight to suddenly take the long way round - instead, any route alterations are communicated to the pilots when they are still on the ground, and if necessary, extra fuel is taken on board.

This flight plan is then given to the pilots, and programmed into the FMC. The FMC is a very sophisticated piece of software, and can in fact calculate the required fuel for this leg all by itself - you can even program it to calculate the best trade-off between efficiency and speed (see this KLM video at 1:40). I'm not entirely sure about the details, but you can probably program the SID and STAR of the departure and destination airports respectively as well. 

On top of this, extra fuel is taken on board for things like taxi, expected holding patterns (your dispatcher will know how busy you can expect it to be at your destination airport; e.g., Heathrow is generally very busy and you might want to take some extra fuel on board), and enough fuel to at least be able to perform a few go-arounds or a diversion if circumstances call for it. In the end, the pilot is responsible for taking enough fuel on board, but will rely on the FMC and dispatch to make an educated guess.