Shakespeare's audiences were typically noisy, restless and unruly, especially those standing in the pit. He liked to catch their attention quickly and quiet them down with a dramatic opening. By having Barnardo ask "Who's there?" he suggests that there is some imminent danger which makes this sentinel especially tense and apprehensive. Francisco's reply also suggests danger and fear.
Nay, answer me. Stand and unfold yourself.
These two sentinels are even frightened of each other. It turns out that there are two reasons for them to be especially wary. One is that there is some danger of an invasion by the army of Fortinbras, Prince of Norway. Denmark is on a warlike footing. The other reason is that these two men have seen a ghost on the previous night, and they are afraid it might come back again. As Bernardo is telling Horatio about their experience, the ghost actually does reappear. So the tension that is created by the opening line, "Who's there?" continues to escalate and to hold the audience in rapt attention. This preliminary dialogue before the ghost appears is effective because it helps to establish a mood of fear and also enables the audience to recognize the ghost as a ghost when it does appear.
A brief addition: since the plays were performed in daylight, the line begins the "suspension of disbelief" by telling the audience that it is nighttime in the play's reality.
What a marvelous opening! It immediately sets the tension in the play. First, as Bernardo enters it is presumably dark. He is actually reporting for his guard duty post and ordinarily the guard on post would make the challenge, "Who's there?'" So who else would it be but Francisco? As we find out later there is a ghost stalking the ramparts. This is the source of Bernardo's ambivalence of what is the sound and faint vision of something before him. Is it the apparition or is it Bernardo?
Shakespeare adds to this uncertainty by having Bernardo rather than Francisco offer the challenge. We (the audience) are not sure who is actually on duty, i.e. who is there. Two actors enter from the opposite sides of the stage our initial impression is that the first to speak is the guard, but it isn't. We are immediately introduced to the theme of image and reality. But one component of this theme is the theme of stagecraft.
The question is posed to us the audience as much as it is to the other actor on stage. Shakespeare has tightly linked the beginning of the play to the end. There is a circularity in the play. As it ends Horatio has requested that this story of Hamlet's be presently performed. Fortinbras directs four captains to "bear Hamlet...to the stage." So it begins as it has for over 400 years.
Lastly, the opening line supports the theme of duality. Other than the ambiguity of the question along with the entrance of two guards and the question being posed to the audience and to the stage, this question is actually delivered twice. At about line 15 as two more characters make there entrance, Francisco finally offers the challenge, "Stand ho! Who is there?"