Thank you for using Just Answer. While all agree that Billy the Kid was killed by Sheriff Garrett. However, there are three versions as to how it happened. Wikipedia
has a good summary of the three versions.
Responding to rumors that McCarty was still lurking in the vicinity of Fort Sumner almost three months after his escape, Sheriff Garrett and two deputies set out on July 14, 1881, to question one of the town's residents, a friend of McCarty's named Pedro Maxwell (son of land baron Lucien Maxwell). Close to midnight, as Garrett and Maxwell sat talking in Maxwell's darkened bedroom, McCarty unexpectedly entered the room. There are at least two versions of what happened next.
One version suggests that as the Kid entered, he failed to recognize Garrett in the poor light. McCarty drew his pistol and backed away, asking "¿Quién es? ¿Quién es?" (Spanish for "Who is it? Who is it?"). Recognizing McCarty's voice, Garrett drew his own pistol and fired twice, the first bullet striking McCarty just above his heart, killing him. In a second version, McCarty entered carrying a knife, evidently headed to a kitchen area. He noticed someone in the darkness, and uttered the words, "¿Quién es? ¿Quién es?" at which point he was shot and killed in ambush style.
Although the popularity of the first story persists, and portrays Garrett in a better light, many historians contend that the second version is probably the accurate one. A markedly different theory, in which Garrett and his posse set a trap for McCarty, has also been suggested. Most recently explored in the Discovery Channel documentary, Billy the Kid: Unmasked, this theory contends that Garrett went to the bedroom of Pedro Maxwell's sister, Paulita, and bound and gagged her in her bed. Paulita was an acquaintance of Billy the Kid, and the two may have considered getting married. When McCarty arrived, Garrett was waiting behind Paulita's bed and shot the Kid.