The high court, which has long been at odds with the civilian government, had convicted Mr Gilani in April of contempt of court for defying its order to reopen corruption cases against Asif Ali Zardari, the president.
The parliamentary speaker, a close associate of Mr Zardari, refused to remove Mr Gilani from office on the ground that the contempt conviction did not require it.
Two opposition leaders challenged the speaker's ruling and the court yesterday found in their favour. Mr Gilani is the first serving prime minister in Pakistan's history to be convicted by a court.
"Yusuf Raza Gilani stands disqualified as a member of the Majlis-e-Shoora [parliament]," the chief justice of the Supreme Court, Iftikhar Chaudhry, told a packed courtroom. "He has also ceased to be the prime minister of Pakistan … the office of the prime minister stands vacant."
Justice Chaudhry ordered Mr Zardari to replace his premier, and said he “should fulfil his constitutional duties to ensure continuation of democratic rule”.
He also directed the Election Commission of Pakistan to issue a notification declaring Mr Gilani ineligible for office.
Immediately after the verdict, Mr Zardari cancelled a planned trip today to Russia and convened the top officials of his ruling Pakistan People’s Party to discuss their options. There was no immediate decision and another meeting of top party officials and their allies was set for late last night.
The ruling party’s chief spokesman, Qamar Zaman Kaira, appeared to defer to the court ruling. “Technically speaking, there is no prime minister after the court verdict,” he said.
“We have reservations on this verdict but we will decide our future line of consultation with our political allies and legal advisers … our workers should stay peaceful.”
However, Fawad Chaudhry, an adviser to the prime minister, said Mr Gilani would continue working and any final decision about his fate would be taken by parliament. “The Supreme Court cannot disqualify the prime minister like this,” he said.
In the past, the party has said it would have the numbers in parliament to elect a new prime minister if Mr Gilani were ousted by the court.
Mr Zardari could dissolve the National Assembly and call early elections, which are due next year, if coalition partners fail to elect a new prime minister.
But some aides have suggested that the prime minister might try to hold on. If Mr Gilani were to resist the order, it could spark constitutional deadlock and social unrest, even raising the possibility of the army staging a coup as it has done three times before.
“If they did not accept the verdict then the situation would not remain in anyone’s control,” said Najam Sethi, a prominent political commentator.
For more than half of its 65 years, Pakistan has been ruled by the military, which is now mired in a prolonged war with Taliban and Al Qaeda-inspired insurgents, some of whom are widely believed to receive support from members of the military itself.
Yesterday’s ruling was the culmination of a process that began with a Supreme Court decision in 2009 ordering the government to ask authorities in Switzerland to reopen the cases against Mr Zardari. Mr Gilani refused on the ground that a serving president had immunity from prosecution, and in January the court ordered contempt proceedings against him.
Mr Gilani has been refusing to step down. He says he has done nothing wrong and accuses the Supreme Court’s chief justice of having a vendetta against him and his party.
The uncertainty is likely to further distract Pakistan’s leaders from tackling a dizzying array of problems, from widespread poverty to a struggling economy.
In the past few days, Pakistanis, furious over power cuts that can last up to 18 hours a day in some areas and have crippled key industries, have burnt tyres in the streets and thrown rocks at buildings.
A fresh political crisis in Pakistan also would be a major concern for the United States, as Islamabad is a key partner in Washington’s efforts to stabilise Afghanistan, despite the current strains in their ties.
* With additional reporting by Agence France-Presse and Reuters