In Hamlet, why does Hamlet hesitate to carry out the commands of the ghost?
Hamlet's hesitation to act is rooted in two different aspects of the drama: Shakespeare's development of Hamlet's very complex character and the conventions of the revenge play, a popular dramatic genre of the time.
In the revenge play, and Hamlet is a revenge play, the plot develops in certain steps. Before the closest blood relative can take revenge for the murder of a loved one, the murder must first be established and the guilty perpetrator identified. These two steps take quite some time in Hamlet, as the young prince must convince himself that the ghost he has seen is not a false vision (an instrument of evil), that his father had indeed been murdered, and that Claudius is indeed guilty of the crime. Hamlet goes to great effort--feigning madness, arranging the play at court, and enlisting Horatio's aid--to make sure that Claudius murdered Old Hamlet. Both Hamlet's and Claudius' elaborate secret plotting, which makes up most of the play, are consistent with the conventions of the revenge play. In other words, if Hamlet had met the ghost and then immediately exacted revenge, there would be no play!
Hamlet's hesitation, however, has been interpreted most often in terms of his own complicated character and his position in Denmark. Denmark is weakened and threatened by foreign invaders, the court is in disarray, and killing a king under any circumstances is a serious matter. To revenge his father, Hamlet must commit regicide, the most serious crime in his society. He hesitates in the face of these circumstances.
Hamlet is also a deeply spiritual person, a student of theology who had been called home from his studies when his father died. The act of murder is a mortal sin. He is trapped between his own moral beliefs and his responsibility to avenge his father's murder. Deeply conflicted, he considers suicide, but self-murder is a sin, as well. Hamlet finds himself in the wrong place at the wrong time, trapped by his circumstances and his own identity.
Finally, Hamlet is deeply depressed. A serious, introspective, philosophical person by nature, his father's death and his mother's marriage to Claudius--an immoral, incestuous act and one of betrayal of Hamlet's father--plunge Hamlet into a state of deep depression. His separation from Ophelia and her subsequent death make his depression even more profound. He becomes obsessed with death and the futility of life. He examines his own character again and again. He is paralyzed, incapable of action throughout most of the drama.
For all of these reasons, Hamlet does not act quickly to exact revenge after meeting the ghost on the battlements.