ITV has agreed the sale of its media and entertainment business to Sky for up to £1.6billion, the companies have announced.