Thames Water has staved off effective government ownership after an unsuccessful legal challenge to its financial restructuring.