Published: 2017-04-20 15:34:56 BdST
A verdict to dismiss Sharif would have left his party in power but would have sparked turmoil at a time when Pakistan is experiencing modest growth and improved security, and the civilian government and powerful military have appeared to come to uneasy terms.
Pakistan's stock market jumped after the ruling with the benchmark index gaining almost 2 percent. Sharif is seen as pro-business.
Two of five judges on the court bench recommended that Sharif should step down but they were out voted.
The court ordered a joint investigation team to be set formed to look into allegations around three of Sharif's four children using offshore companies to buy properties in London.
Sharif and his children deny any wrongdoing.
The team has two months to complete its inquiry and its findings could yet weaken Sharif as he heads into a general election, due by May 2018.
But for now, Sharif and his ruling Pakistan Muslim League-Nawaz, are celebrating.
Sharif's daughter and his presumptive political heir, Maryam Nawaz Sharif, posted a photo on her Twitter account of the family welcoming the court's decision.
"We are ready for all kinds of investigation," Sharif's defence minister, Khawaja Asif, told reporters outside the court.
The Supreme Court agreed last year to investigate the Sharif family's offshore wealth after opposition leader Imran Khan threatened street protests following the leaking of the "Panama Papers".
Khan called on Sharif to step down until the investigation was completed.
"The prime minister should immediately resign at least for the 60 days until JIT completes its work," Khan told reporters, referring to the joint investigation team.
"How can he continue as prime minister when he is being investigated? What moral grounds does he have to continue in office?"
Documents leaked from the Panama-based Mossack Fonseca law firm appeared to show that Sharif's daughter and two sons owned offshore holding companies registered in the British Virgin Islands and used them to buy properties in London.
The opposition accuses Sharif of failing to explain the source of offshore money and of lying to parliament.
Sharif, one of Pakistan's richest men, told parliament last year that his family wealth was acquired legally in the decades before he entered politics.
Farogh Naseem, a Supreme Court lawyer and sitting senator, told Reuters after the ruling that Sharif could breathe easy, for now.
"It was a close call for the prime minister," he said. "There is no clean chit for the prime minister, but for the time being he has been saved from being disqualified."
But Naseem said that with three of the institutions involved in the joint investigation under Sharif's control, a decision against him was unlikely.
Khan's party has indicated that it would not launch a new street protests if it was disappointed by the judgement. A party spokesman was not immediately available for comment after the ruling.
Pakistani politicians of all stripes have over the years been accused of corruption, which remains endemic.