The child, now two, had been placed with foster carers after her parents were unable to care for her.
A High Court judge then ruled the youngster should be cared for by the grandparents - her father's parents.
But the foster couple hoped to adopt her and brought a challenge. The Family Division of the High Court in Newcastle has ruled in their favour.
The four-day hearing was described by Mr Justice Cobb as being "as difficult as any" to come before the family court.
He said the grandmother was 58 and the grandfather was 60 while the couple who wanted to adopt, who he called Mr and Mrs X, were in their 30s.
The grandparents had seen little of their son for the past decade and had never met his partner, both of whom had learning difficulties.
When the child was born she was placed with foster carers, and when council social services staff had not traced any relatives a judge ruled she should be placed for adoption.
Mr and Mrs X, who had been looking after her since December 2014, then made an application to do so.
However, in June 2015 the girl's biological parents, who had moved to another area, had a son.
Social services staff from the second council searched for relatives and traced the grandparents.
It was only then that the older couple learned about the existence of their granddaughter.
A High Court judge ruled she should be cared for by them, but Mr and Mrs X challenged this in the Court of Appeal, which ordered a review.
In the latest hearing, Mr Justice Cobb said experts agreed the child would suffer "trauma, distress, confusion and emotional harm" if "uprooted" from the home of Mr and Mrs X.
While this had to be weighed against the possibility of her suffering long-term harm and "feelings of rejection"  if she stayed with her adopted family, he had reached the "clear conclusion" that this would be in the child's best interests.
He said: "Inevitably, as much as this outcome will provide great joy to the Xs, it will generate profound disappointment to the paternal grandparents."
Neither councils could be named, and Mr Justice Cobb added:  "I should record that the paternal grandparents express anger at the [first] local authority for failing to take the relevant steps to trace them when proceedings were afoot in relation to [the girl] in 2014.
"They feel, understandably, that things could have been so different had rudimentary steps been taken."
A couple have lost their fight to adopt a granddaughter they did not know existed until she was a year old.