DOHA, Qatar—The U.S. and the Taliban signed a historic deal Saturday meant to end America’s longest war and pave the way for Afghan factions to bring 18 years of costly fighting in their country to an end.

To the shouts of “God is great” by Taliban leaders, a co-founder of the insurgent group and a top American diplomat shook hands on a plan that calls for the U.S. to pull all its forces out of Afghanistan over the next 14 months. In exchange, the Taliban pledged to discuss a long-term cease-fire with the Afghan government...