World leaders have signed off on an agreement which reaffirms a basic commitment by the world’s biggest economies to multilateral trade and a “rules-based international order”, but bows to US demands for urgent reform of the World Trade Organisation (WTO).

Agreement on a joint communique was by no means guaranteed at the beginning of the summit, when US negotiators took hardline positions, rejecting any mention of multilateralism, the “rules-based order” or the role of the International Monetary Fund (IMF) as a global safety net.

Failure to agree would have called into question the survival of the G20 as a forum for maintaining global economic stability, and could have accelerated the slide towards protectionism.

France’s president, Emmanuel Macron, declared a victory for restoring international institutions at a moment when “multilateralism is going through a real crisis”. European unity on basic principles had helped save the summit, he said.

A senior White House official, who declined to speak on the record, called the summit a success in part because the US had been able to exclude language on multilateralism, and because “for the first time ever, the G20 recognized the WTO is currently falling short of meeting its objectives and that it’s in need of reform”.

Sherpas from the European Union and 19 countries worked through the week to salvage a meaningful joint statement, culminating in an all-night session on Friday. While the joint text retains some basic principles on trade and global governance, it has no more than a passing mention of migration and refugees and reflects no progress on climate change from last year’s summit in Hamburg.

As in Hamburg, one paragraph in the compromise reflects the US sceptical view, and another – acknowledging the seriousness of the threat and the need for concerted effort to combat it – reflects the views of all the other G20 leaders.

Speaking off the record, a senior US official told reporters the US “specifically preserved and explained our position for why we’re withdrawing from the job-killing Paris agreement”. The official claimed to have seen signs of “the coalition fraying” among some signatories to the Paris deal, “like Turkey, like Saudi Arabia, like Russia”.

There had been fears that the final text could be vetoed by any of the leaders and there is concern that Donald Trump, who did not take part in a session on trade on Friday, could disown the agreement at the last moment. Its first test came in a working dinner between Trump and Xi Jinping of China on Saturday night, at which the two leaders sought to resolve trade disputes with the threat of an all-out trade war hanging over their heads.

“We’ll be talking about a thing called trade,” Trump told reporters ushered into a bilateral meeting with Angela Merkel of Germany. “It’s a very important meeting.”

In the wake of new revelations in the investigation of his campaign’s links with the Kremlin, Trump has acted erratically throughout the two-day summit, dropping bilateral meetings and announcing he would hold a press conference only to change his mind a few hours later, citing the death of former president George HW Bush.

Trump cancelled a meeting with Russian president Vladimir Putin. The White House confirmed the two leaders nonetheless spoke at dinner on Friday.

John Bolton, far right, talks to the US secretary of state, Mike Pompeo, as Donald Trump looks on. Photograph: Pablo Martínez Monsiváis/AP

Trump’s national security adviser, John Bolton, a lifelong opponent of multilateralism and its institutions, had pushed for a hardline position, particularly on the role of the IMF. But European officials led a pushback, arguing that if the agreement on common principles was watered down too much, the statement would be meaningless, the G20 robbed of its purpose.

“For some of us around the table, we couldn’t backtrack on the DNA of the G20,” a European official said.

The compromise language keeps the basic principles on which most of the leaders in Buenos Aires insisted, in return for acknowledgment of problems in the international trading system and a commitment to address them. In particular they pledged to reform the WTO, which sets the rules for global trade and provides a forum for resolving disputes. The leaders agreed to make significant progress on reform before the next G20, in Osaka next June.

The statement begins with a statement of principle the Europeans insisted on but the US found hard to swallow. It reads: “We renew our commitment to work together to improve a rules-based international order that is capable of effectively responding to a rapidly changing world.”

The US delegation had opposed the positive reference to a “rules-based international order” as they argued the current system is skewed against the US and has allowed China to get away with unfair trading practices. For the same reason, the US opposed references to the threat of protectionism to global growth, insisting Trump’s use of tariffs is a legitimate response to a skewed playing field.

A crucial paragraph on trade expresses the compromise.

“International trade and investment are important engines of growth, productivity, innovation, job creation and development,” the communique says. “We recognise the contribution that the multilateral trading system has made to that end. The system is currently falling short of its objectives and there is room for improvement.”

The last dispute to be resolved, at 6.30am on Saturday, was over refugees and migration, with the US opposing references to the role of multilateral organisations in dealing with the issue and the responsibility of wealthy countries to mitigate the human cost.

The compromise puts off addressing the crisis until the next year, when Japan will hold the G20 presidency. The paragraph on refugees simply says they are a “global concern” and that “shared actions” to addresses the causes and consequences of displacement are “important”.

“This is the minimum,” a European official said, arguing that to say nothing about the issues could set a precedent and remove them permanently from the G20 agenda. “The choice was really we do nothing. We will not hide our disappointment but at least we have something.”