South Africa brought the case to the International Court of Justice (ICJ) earlier this month, asking it to grant emergency measures to halt the fighting, which has killed more than 26,000 Palestinians.
It accused Israel of state-led genocide in its offensive, begun after Hamas militants stormed into Israel killing 1,200 and kidnapping more than 240. Israel sought to have the case thrown out.
In Friday's ruling, the judges said Israel must take all measures within its power to prevent its troops from committing genocide, punish and must take steps to improve the humanitarian situation.
While the ICJ did not order a ceasefire, it said it would not throw out the genocide case, ruling that the Palestinians appeared to be a protected group under the 1948 Genocide Convention. It did not decide the merits of the genocide allegations.
Israel has called South Africa's allegations false and "grossly distorted", and said it makes the utmost efforts to avoid civilian casualties.