The long-awaited agreement is a political triumph for Russian President Vladimir Putin, who is courting partners in Asia as those in Europe and the United States seek to isolate him over Moscow's annexation of Ukraine's Crimea peninsula.
Commercially, much depends on the price and other terms of the contract, which has been more than a decade in the making.
China had the upper hand as talks entered the home stretch, aware of Putin's face-off with the West.
But both sides could take positives from a deal that will directly link Russia's huge gas fields to Asia's booming market for the first time - via thousands of miles of new pipeline across Siberia that form part of the package.
"This is the biggest contract in the history of the gas sector of the former USSR," said Putin, after the agreement was signed in Shanghai between state-controlled entities Gazprom and China National Petroleum Corp (CNPC).
"Our Chinese friends are difficult, hard negotiators," he said, noting that talks went on until 4 a.m.
"Through mutual compromise we managed to reach not only acceptable, but rather satisfactory, terms on this contract for both sides. Both sides were in the end pleased by the compromise reached on price and other terms," the president said.
Putin and Chinese counterpart Xi Jinping applauded as they witnessed the deal being signed before the Russian leader was to leave Shanghai at the end of a two-day visit.
The agreement came in time for a major economic summit in St. Petersburg starting Thursday. About a dozen chief executives and chairmen of major U.S. and European firms have withdrawn from the forum over the Ukraine crisis.
Putin loyalist and senior parliamentarian Alexei Pushkov, who was included on a U.S. list people placed under sanctions imposed after the crisis in Ukraine, said the gas deal showed Russia could not be isolated.
"B. Obama should abandon the policy of isolating Russia: it will not work," he tweeted, referring to U.S. President Barack Obama, who has pushed for greater Western punishment of Russia.
In Washington, State Department spokeswoman Jen Psaki said the gas deal would not undermine efforts by the United States to pressure Moscow not to meddle in Ukraine's affairs.
"Whether this specific deal, if there is a deal, would (have an) impact, I don't think we're prepared to jump to that conclusion," Psaki told reporters. "We still believe we have a range of tools at our disposal," she added.
The United States and European countries have warned the Kremlin that it will face broader sanctions, possibly involving new investment in key areas of the Russian economy, if it meddles in Ukraine's elections later this month.
Gazprom CEO Alexei Miller declined to say at what price the deal was struck, but sources at the companies involved said Gazprom refused to go below $350 per thousand cubic metres.
That compares to a price range of $350-$380 most European utilities pay under discounted long-term contracts signed in the last two years. Putin said the formula was similar to the European price tied to the market value of oil and oil products.
For China, the implied price is crucially below the Asian cost of importing liquefied natural gas (LNG), an alternative energy source it is developing.
Increased gas imports will also help Beijing in its declared "war on pollution" aimed at reducing its reliance on coal which contributes to the harmful smog shrouding major cities.
Another potential sticking point in talks was whether China would pay a lump sum up front to fund considerable infrastructure costs.
According to Putin, China will provide $20 billion for gas development and infrastructure, but Miller said the two sides were still in talks over any advance.
The gas will be transported along a new pipeline linking Siberian gas fields to China's main consumption centres near its coast. Russia will begin delivering from 2018, building up gradually to 38 billion cubic metres (bcm) a year.
Russia plans to invest $55 billion in exploration and pipeline construction up to China, and CNPC said it would build the Chinese section of the pipeline.
EUROPE STILL CRUCIAL
The contract with CNPC does not mean Russia is giving up on Europe. Last year, Gazprom supplied western Europe and Turkey with over 160 bcm of gas, dwarfing intended deliveries to China.
And for their part, European consumers cannot easily switch from Russian gas even if they want to.
Beyond supplying China with gas via a pipeline, the 30-year deal opens up an opportunity for Gazprom to become a bigger player in the booming Asian LNG market, a sector it has so far not been involved in on a major scale.
Gazprom is planning to build a new LNG plant on Russia's Pacific coast near Vladivostok, but so far lacks the infrastructure to supply the facility with the amounts of gas necessary to meet demand in the region.
The pipeline to China would change this, ideally positioning Gazprom's Vladivostok terminal close to the leading LNG buyers of Japan and South Korea as well as the rising market on China's eastern coast.
Shares in Gazprom rose about 2 percent after the deal was announced, and closed up nearly 1 percent.