The government appealed against the judgement, saying it could lead to assisted-suicide legislation that would be open to abuse
Last year, the High Court in Pretoria had ruled that a doctor could help retired advocate Robin Stransham-Ford, 65, who suffered from prostate cancer, to end his life.
Stransham-Ford however died just hours before the court granted the order.
The government appealed against the judgement, saying it could lead to assisted-suicide legislation that would be open to abuse.
On Tuesday, the Supreme Court of Appeal (SCA) sided with the government as the grounds for granting the order did not exist when it was handed down since Stransham-Ford had already died.
The High Court was not informed of Stransham-Ford's death before its judgement.
The Supreme Court said there had been "no full and proper examination of the present state of our law in this difficulty area" and that the order was made on an "incorrect and restricted factual basis."
"A court addressing these issues needs to be aware of differing cultural values and attitudes within our diverse population," the Supreme Court said in its ruling.
Justice Minister Michael Masutha welcomed the appeal victory.
"We are relieved that the SCA correctly stated the law as this decision could have far reaching implications on the constitutionally entrenched right to life and our common law crimes of murder and culpable homicide," he said.
Medically-assisted suicide or voluntary euthanasia is illegal in South Africa, but in recent years there have been growing calls for it to be legalised.
Dignity SA, which lobbies for assisted suicide legislation, expressed disappointment at the court's decision.
"The (High) court was ruling on the right of a terminally ill man, who was suffering unbearably at the end of his life," Dignity SA founder Sean Davison told AFP.
"I do not comprehend how anybody could oppose a person like that. This is a human rights issue."
He said the group was considering taking the case to the Constitutional Court.
South African retired Anglican archbishop and anti-apartheid icon Desmond Tutu celebrated his 85th birthday this year saying he would like to be allowed the option of dignified assisted death.
"I have prepared for my death and have made it clear that I do not wish to be kept alive at all costs," Tutu wrote in an article.
The Netherlands and neighbouring Belgium became the first countries in the world to legalise euthanasia under strict conditions in 2002.