Thames Water, which was due to run out of money, has been given a lifeline after a £3bn loan was approved by the High Court.