HONG KONG — Hong Kong’s highest court ruled unanimously on Monday that a woman from the Philippines who had lived and worked here for nearly 27 years as a domestic helper was not entitled to permanent residency, ending an acrimonious legal fight over the immigration rights of migrant workers.

Public opinion surveys had shown that a large majority of Hong Kong citizens opposed granting permanent residency to the city’s domestic helpers, which would give them the right to live here for the rest of their lives and use the nearly free public health care system and other social services. Many residents and the local government feared that granting permanent residency would have resulted in increased social spending.

But activists for domestic helpers, along with Mark Daly, the lawyer who championed their cause in the legal battle, contended that denying permanent residency would cement lasting divisions in Hong Kong based on gender and race. Virtually all of the estimated 300,000 foreign domestic helpers in this city of seven million are women, and they are not mainland Chinese, who qualify for separate rules for permanent residency and passports under Hong Kong’s Basic Law, the city’s miniconstitution.

The decision on Monday by the Court of Final Appeal, Mr. Daly said, “is a regrettable decision; it basically entrenches their being second-class citizens.”