Mrs Justice Lieven has concluded that a pregnancy termination is in the woman's best interests after analysing evidence at a hearing in the Court of Protection, where issues relating to people who lack the mental capacity to make decisions are considered, in London.
Bosses at an NHS trust responsible for the woman's care had asked Mrs Justice Lieven to let doctors perform an abortion.
Specialists said a pregnancy termination was the best option.
The woman's mother, a former midwife, was against abortion because of her Catholic faith and said she could care for the child.
A social worker who works with the woman said the pregnancy should continue.
Lawyers who represented the woman also said she should be allowed to give birth.
But Mrs Justice Lieven said a balance of evidence showed that termination was the best option.
The judge said some evidence in the case was "heartbreaking".
She heard that the woman, who is in her twenties and is 22 weeks pregnant, had the mental age of a child aged between six and nine.
Mrs Justice Lieven had considered the terms of the 1967 Abortion Act, which says termination may be performed up to the 24th week of pregnancy, and the 2005 Mental Capacity Act before reaching a decision.
She analysed the case at a public hearing but said the woman, who lives in the London area, cannot be identified in reports of the case.
The judge also said the NHS trust which asked for a decision, and the council which employs the social worker, cannot be named because publication of their names might create an information jigsaw which could lead to the woman's identity being revealed.
She had heard that the woman had been diagnosed with a "moderately severe" learning disorder and a mood disorder.
The judge had also been told that the "circumstances of the conception" were "unclear" and a police investigation was ongoing.
Mrs Justice Lieven said she had to make an "enormous" decision on the basis of what was in the woman's best interests.
The judge said the woman did not have the mental capacity to make the decision although evidence indicated that she wanted to keep the baby.
"I am acutely conscious of the fact that for the State to order a woman to have a termination where it appears that she doesn't want it is an immense intrusion," said the judge in a ruling on Friday.
"I have to operate in (her) best interests not on society's views of termination."
The judge said the woman had no sense of what having a baby "meant" and added: "I think she would like to have a baby in the same way she would like to have a nice doll."
Mrs Justice Lieven was told that the woman could not care for a baby alone.
The woman's mother said she could care for the baby - and her daughter could help.
But Mrs Justice Lieven doubted whether social workers would tolerate such a situation because of the risks posed by the woman's behavioural and psychological problems.
She said if the baby stayed with the woman's mother, the woman might have to leave her mother and leave home.
The baby might also have to live with foster carers or be placed for adoption.
Mrs Justice Lieven said she thought the woman would suffer more distress if the baby was taken away than if pregnancy was terminated.
"I think (she) would suffer greater trauma from having a baby removed," said the judge.
"It would at that stage be a real baby."
The judge added: "Pregnancy, although real to her, doesn't have a baby outside her body she can touch."
Stay up to date with the latest news stories from a Christian perspective. Sign up to our daily newsletter and receive more stories like this straight to your inbox every morning.