LONDON — The parents of Charlie Gard, the chronically ill British infant whose plight drew attention from Pope Francis and President Trump, on Monday gave up their wrenching legal effort to artificially prolong his life, bowing to the consensus of medical experts who said there was no realistic chance of saving him.

Breaking into tears as she stood in front of the court, the child’s mother, Connie Yates, said that she and her husband, Chris Gard, “only wanted to give him a chance of life.” She added that “we have decided to let our son go” because there was no prospect of meaningful improvement in his quality of life.

The High Court judge hearing the case, Nicholas Francis, praised the parents on Monday for their determination to help their son. “No parent could have done more for their child,” he told them, adding that the only appropriate course was “to let him die with dignity.”

Many in the courtroom wept.

By withdrawing their last-ditch pleas to compel the hospital that is treating their son to subject him to an experimental therapy, the parents effectively ended a wrenching bioethical case. “Time has run out,” the parents’ lawyer, Grant Armstrong, told the court.

Charlie has a rare genetic abnormality known as mitochondrial DNA depletion syndrome. He cannot see, hear, swallow or cry.

Great Ormond Street Hospital, the London pediatric institution that has been treating Charlie since October, has argued for months that letting the child die was the only humane course of action. It won a series of court rulings allowing it to turn off life support for Charlie.

“We are more sorry than I have words to say,” the hospital’s lawyer, Katie Gollop, told the parents in court, expressing compassion. The hospital has not specified a timeline for shutting off the life support, saying only that it would do so in consultation with the parents.

At the center of the controversy was an experimental therapy suggested by Dr. Michio Hirano, a neurologist at Columbia University Medical Center, which has helped children with a less severe form of Charlie’s condition.

Dr. Hirano had held out the prospect that the treatment, known as nucleoside bypass therapy, might offer a slim chance of improving the child’s ability to breathe, though he also acknowledged that Charlie had suffered significant brain damage.

The treatment pioneered by Dr. Hirano — who was initially known in court papers only as Dr. I, until the judge agreed recently to allow journalists to report his identity — has been tested on mice and on 18 people with a mutation in the TK2 gene.

But it has never been tried on someone with Charlie’s particularly severe form of mitochondrial DNA depletion syndrome, which is caused by a different mutation, in a gene called RRM2B.

Last week, Dr. Hirano traveled to London to assess the child — he had not previously done so — and doctors conducted an electroencephalogram, a test that detects electrical activity in the brain.

The consensus was that Charlie suffered muscular atrophy and damage that would be irreversible even with the experimental treatment.

“For Charlie it is too late, the damage has been done,” Mr. Armstrong told the court on Monday, adding that the parents, “having made this most painful of decisions,” now wished “to spend as much time as possible with Charlie.”

Ms. Yates said that “time had been wasted,” and that the therapy should have been tried earlier. But she said she was glad that she and her husband had brought their case, which she said raised important ethics issues and showed that her son’s life was not in vain.

“We will have to live with what-ifs which will haunt us for the rest of our lives,” she told the court, her voice breaking. “We have not kept him alive out of selfishness.”

The hospital never changed its view that Charlie’s brain damage was irreversible, and that the experimental nucleoside therapy would be “futile” and merely prolong the child’s potential suffering. (Whether Charlie feels pain was one of many points of dispute.)

However, after the pope and Mr. Trump weighed in, the hospital agreed to refer the matter once more to the High Court judge, in deference to the parents’ wishes that Dr. Hirano’s proposed treatment be given a hearing.

The case has become so heated that protesters have rallied outside the hospital, and hospital employees have received death threats.

Over the weekend, the parents issued a statement condemning the threats, and their lawyer, Mr. Armstrong, said on Monday that it would be “unproductive to continue protests” at the hospital.

“Any parent would have fought as hard as Chris Gard and Connie Yates,” he said of his clients, who hugged relatives before entering the courtroom.

The father, Mr. Gard, had held Charlie’s toy monkey throughout the legal proceedings, but did not do so on Monday. His wife sobbed.

Their lawyer, Mr. Armstrong, said the case was worthy of a Greek tragedy, and he did not spare the hospital from some final criticisms. He suggested that the experimental therapy might have had some potential had it been tried when the parents first became aware of it late last year, but that it was now too late.

Addressing the argument that the treatment could have made a difference if tried sooner, Ms. Gollop said the hospital had looked into it but concluded months ago that Charlie had suffered “irreversible neurological damage.”

The decision to end the battle came after heated and rancorous proceedings.

On Friday, after a lawyer for the hospital said Charlie’s latest brain scan made for “sad reading,” Mr. Gard yelled, “Evil!” and Ms. Yates burst into tears. “We haven’t even read it,” she said.

On Monday, Ms. Yates struck a more conciliatory tone, thanking the staff at the hospital, as well as people who have rallied in support of the parents and their son.

Addressing her son, she said, “We love you so much,” adding: “Mommy and Daddy love you so much Charlie and we’re sorry we couldn’t save you. Sweet dreams little boy.”

Justice Francis, reviewing the case, noted the global attention it had received, from Mr. Trump, the pope, Prime Minister Theresa May and others. He said that a pitfall of today’s social media environment was that when difficult cases get prominent attention, commentators give opinions without regard to fact.

“If a doctor is to give evidence,” that doctor should first see the patient, Justice Francis said, in what seemed an oblique reference to Dr. Hirano.

Justice Francis commended Dr. Hirano for eventually coming to London. He chastised those who had threatened hospital workers. And he said the notion that Charlie had been a “prisoner” of Britain’s National Health Service was “absurd.”

In most cases, he said, parents have the main say over the best way to treat their children, and only in rare cases do courts get involved.

Dr. Dominic Wilkinson, a neonatologist and professor of medical ethics at Oxford University, who is not involved in the case, noted that hospital ethics committees are reluctant to authorize untested treatments on a terminally ill baby, who cannot offer consent.

“It is understandable that the parents want to hold on to the child,” Dr. Wilkinson said in a phone interview. “But there are limits to a treatment parents are able to request for a child if the risks are too great.”