ITV has agreed a £1.6bn deal with Sky, the owner of Sky News, for its media and entertainment arm following months of talks.