The deal constitutes an important political victory for Mr. Trump and his second trade win of the month. The president signed an initial trade pact with China at the White House just two weeks ago, giving him crucial talking points as he heads into his re-election campaign. While his deals with China and other countries like Japan and South Korea are smaller than traditional trade agreements, Mr. Trump will be able to claim that he has renegotiated trade terms with countries responsible for more than half of American trade.

The president wasted little time in touting the new North American trade deal, calling it a “colossal victory” for farmers and factory workers and the “largest, fairest, most balanced and modern trade agreement ever achieved.”

Mr. Trump has long derided the original NAFTA, and he frequently threatened to rip it up entirely if Canada, Mexico or congressional Democrats would not agree to his new rules.

He came into office with an executive order drafted to begin the process of withdrawing from NAFTA and nearly signed it on several occasions. But more moderate advisers and business contacts repeatedly dissuaded the president from scrapping the deal.

The 26-year-old agreement, which was negotiated by the George Bush administration and signed into law by President Bill Clinton, has since become a political target, derided for encouraging American companies to move factories and jobs to Mexico.

Many economists have a more sanguine view of NAFTA’s legacy, saying the deal provided a positive, if small, benefit to American wages and employment. It allowed industries to reorganize their supply chains around North America and take advantage of the differing resources and strengths of the three countries. The deal helped to more than triple America’s trade with Canada and Mexico.

But the opening of borders has come at a cost. Some Americans, particularly those with less education, lost out as factories moved to Mexico, taking jobs with them.