Justice Nicholas Francis ruled with the "heaviest of hearts" but "complete conviction" that life support treatment in London for eight-month-old Charlie Gard should be ended.
The baby boy suffers from a rare genetic condition and has brain damage, from which he will not recover according to experts consulted by the court.
The judge's ruling was met with a scream of "no!" and Charlie's parents, Connie Yates and Chris Gard, wept as the decision was announced.
Specialists at Great Ormond Street Hospital, where the baby is being treated, had asked the judge to rule it is legal to withdraw life-support treatment.
Francis had visited baby Charlie in hospital and during the ruling praised staff there for the "extraordinary care" provided to the child and his family.
The child's parents had hoped to take him to the US where he would undergo a treatment trial for his form of mitochondrial disease.
More than $1.5 million (1.4 million euros) was raised online for the treatment, through more than 80,000 donations.
The family's lawyer Laura Hobey-Hamsher said they were "devastated" by the court's decision and would consider appealing.
(Except for the headline, this story has not been edited by NDTV staff and is published from a syndicated feed.)