We can visualize it something like this. Peter was brought into the courtyard of the high priest by a disciple who knew the high priest, probably John. As Peter came in he was recognized by the girl who kept the door, and although she didn't object to Peter’s presence initially, she most likely followed him into the courtyard where he had stopped to warm himself at a fire that was there. She asked her question at that point. John does not actually say that she asked her question at the door, only that it was asked by the girl who was “at the door” (v. 17). After this various questions were asked by different people, at the fire and near the outer gateway, leading to Peter’s second denial. Things seem to have settled down then, but sometime after this (Luke says, “about an hour later”), as the trial was drawing to a close, those who were in the courtyard accosted Peter again, among them the relative of the man Peter had attacked, who asked, “Didn't I see you with him in the olive grove” (John 18:26).
We will miss a great deal about Peter’s denial of Jesus unless we see it in its context. For Matthew’s arrangement of material is never arbitrary, as we have seen many times already. In the case of this account, there are two matters of context worth noting.
Was Jesus truly worthy of death? Assuming that a prima facie case of guilt had been made, as it seems to have been in spite of the evidence having been illegally obtained, what should have been the next step under law? Clearly the Sanhedrin should have begun to inquire diligently into the truth or falsity of the claim. We might think that the very nature of Jesus’ claims would have put them beyond any meaningful investigation. But that is not the case. The scribes were masters of the Old Testament. The elders were charged with the defense of anyone in danger of being put to death. They should have asked whether Jesus’ claims matched what the Old Testament taught concerning the Messiah. If they had investigated Jesus’ case fairly, they might have discovered: 
At this point Caiaphas revealed the shrewdness for which the Romans had undoubtedly made him the chief Jewish ruler. What he did was illegal. The high priest was forbidden to intervene in a capital trial, and he could only cast his vote after the other court members had cast theirs. Nevertheless, what he did was a stroke of political genius. Seeing that the case was dissolving, Caiaphas suddenly turned to the prisoner and demanded on the basis of the most solemn oath in Israel, the Oath of the Testimony, “I charge you under oath by the living God: Tell us if you are the Christ, the Son of God” (v. 63).

There were many illegalities in Christ’s trial, among them the arrest and trial by night, the use of a traitor to identify and secure Jesus, the absence of any formal charge, the rushed one-day duration of the trial, the intervention of the high priest in the proceedings, the lack of a defense, and the unanimous verdict. But underneath these many illegalities ran a strong undercurrent of adherence to certain points of law. Most obvious was the calling of witnesses, Matthew indicates what was happening when he records, “The chief priests and the whole Sanhedrin were looking for false evidence against Jesus so that they could put him to death. But they did not find any, though many false witnesses came forward” (vv. 59,60).