Goldman Sachs has agreed to a $5 billion civil settlement with federal prosecutors and regulators over claims relating to its sale of bad mortgage securities to investors, which contributed to the financial crisis seven years ago.