Why is Thames Water in so much trouble?

The UK’s biggest water firm has just secured a rescue loan, but how did it come to this?