Thames Water has won approval to borrow £3 billion to prevent imminent collapse, in a high-cost loan deal described by a judge as “eye-watering”.