GlaxoSmithKline Plc will pay Germany’s Merck KGaA as much as 3.7 billion euros ($4.2 billion) to get access to a promising immune-oncology treatment that the drugmakers see as a key to bringing the next wave of cancer therapies to patients.

The collaboration, which includes an upfront payment of 300 million euros, is the latest step in Glaxo Chief Executive Officer Emma Walmsley’s effort to rebuild the U.K. company’s position in oncology. For Merck, it helps fulfill CEO Stefan Oschmann’s goal to bring in expertise and funds needed to develop and market innovative treatments.

Companies including Roche Holding AG, Bristol-Myers Squibb...