The police and rescue services have been on site in Stilfontein, about 14 miles southwest of Johannesburg, for several days.
The police said earlier in the week that it was likely to involve hundreds of illegal miners, and that they risked being arrested if they came to the surface. The police have also cut off food and water supplies to those believed to be underground.
The police called in experts on Saturday to determine whether a forced evacuation could be carried out, but the court ruling has put a stop to it.
"All miners trapped in the mine shaft must be allowed to come out; no personnel not belonging to the rescue service may enter the mine shaft", the court writes in its ruling.
Thousands of illegal miners, many of them from other countries, are said to be working in abandoned mine shafts in mineral-rich South Africa. The miners are locally known as "zama zamas" - "those who try" in Zulu.