A deal has been agreed in the long-running dispute over a new junior doctors' contract in England.