Campaigners who brought the case against the Scottish government hugged each other and punched the air as they left the courtroom, with several of them in tears.
The Equality Act provides protection against discrimination on the basis of various characteristics, including "sex" and "gender reassignment".
Judges at the Supreme Court in London were asked to rule on what that law means by "sex" – whether it means biological sex, or legal, "certificated" sex as defined by the 2004 Gender Recognition Act.
The Scottish government argued the 2004 legislation was clear that obtaining a GRC amounts to a change of sex "for all purposes".
For Women Scotland argued for a "common sense" interpretation of the words man and woman, telling the court that sex is an "immutable biological state".
Speaking outside the Supreme Court following the ruling, For Women Scotland co-founder Susan Smith said: "Today the judges have said what we always believed to be the case, that women are protected by their biological sex.
"Sex is real and women can now feel safe that services and spaces designated for women are for women and we are enormously grateful to the Supreme Court for this ruling."
First Minister John Swinney said the Scottish government accepted the judgement.
He posted on social media: "The ruling gives clarity between two relevant pieces of legislation passed at Westminster.
"We will now engage on the implications of the ruling."
Swinney added: "Protecting the rights of all will underpin our actions."
A Scottish government spokesperson insisted ministers had acted "in good faith" during the legal proceedings, and noted that the Equality and Human Rights Commission was updating its guidance in response to the judgement.
A UK government spokesman said the ruling would bring "clarity and confidence for women and service providers such as hospitals, refuges, and sports clubs".
"Single-sex spaces are protected in law and will always be protected by this government," the spokesman added.
Conservative leader Kemi Badenoch described the ruling as a "victory for all of the women who faced personal abuse or lost their jobs for stating the obvious".
She added: "It's important to be reminded the court strongly and clearly re-affirmed the Equality Act protects all trans people against discrimination, based on gender reassignment, and will continue to do so."
'Deep concern'
Harry Potter author JK Rowling posted on social media: "It took three extraordinary, tenacious Scottish women with an army behind them to get this case heard by the Supreme Court and, in winning, they've protected the rights of women and girls across the UK."
But Scottish Green MSP Maggie Chapman, a prominent campaigner for trans-rights, said: "This is a deeply concerning ruling for human rights and a huge blow to some of the most marginalised people in our society.
"It could remove important protections and will leave many trans people and their loved ones deeply anxious and worried about how their lives will be affected and about what will come next."
For Women Scotland had warned that if the court sided with the Scottish government, it would have implications for the running of single-sex spaces and services, such as hospital wards, prisons, refuges and support groups.
Transgender people warned the case could erode the protections they have against discrimination in their reassigned gender.
Scottish Trans manager Vic Valentine said the organisation was "shocked" by the court ruling, arguing that it "reverses 20 years of understanding on how the law recognises trans men and women with gender recognition certificates".
They added: "This judgement seems to suggest that there will be times where trans people can be excluded from both men's and women's spaces and services.
"It is hard to understand where we would then be expected to go – or how this decision is compatible with a society that is fair and equal for everybody."
The case follows years of heated debate over transgender and women's rights, including controversy over transgender rapist Isla Bryson initially being put in a women's prison and an ongoing employment tribunal involving a female NHS Fife nurse who objected to a transgender doctor using a women's changing room.
NHS Fife said it would "carefully consider" the court's judgement.
'Biological' or 'certified'?
The judges ruled that interpreting sex as "certificated" rather than "biological" would "cut across the definitions of man and "woman and thus the protected characteristic of sex in an incoherent way".
They said a "certified" definition of sex would weaken protections for lesbians, citing the example of lesbian-only spaces and associations as it would mean that a trans woman who was attracted to women would be classed as a lesbian.
The ruling found the biological interpretation of sex was also required for single-sex spaces to "function coherently".
It cited changing rooms, hostels, medical services and single-sex higher education institutions.
The judges noted "similar confusion and impracticability" had arisen in relation to single-sex associations and charities, women's sport, public sector equality and the armed forces.
The judges added: "The practical problems that arise under a certificated sex approach are clear indicators that this interpretation is not correct."
Gender reassignment is a protected characteristic in law, making it illegal to discriminate against someone on the basis that they are transgender.
Dr Nick McKerrell, senior law lecturer at Glasgow Caledonian University, said the ruling means a transgender women with a GRC who was excluded from a single-sex space would be unable to argue she is being discriminated against as a woman.
He also said the ruling implied that workplaces would need to provide separate spaces for people on the basis of biological sex.
But the law lecturer said arguments over access to single-sex spaces would not be "settled" by this court case.
He told the BBC: "It doesn't mean everything overnight is going to change in terms of stopping trans people from accessing services. It will depend on what providers think the new definition will mean for them."
Dr McKerrell said the judgement does not immediately change anything for the rules on transgender participation in women's sport, but that it might prompt a "reassessment" of rules.
How did we get here?
The legal dispute began in 2018, when the Scottish Parliament passed a bill designed to ensure gender balance on public sector boards.
For Women Scotland complained that ministers had included transgender people as part of the quotas in that law.
The issue has been contested several times in the Scottish courts.
Holyrood ministers won the most recent case in Scotland, with judge Lady Haldane ruling in 2022 that the definition of sex was "not limited to biological or birth sex".
The Scottish Parliament passed reforms that year that would have made it easier for someone to change their legally recognised sex.
The move was blocked by the UK government, and has since been dropped by Holyrood ministers.