The ghost's appearance has several purposes. Most importantly, it sets the plot into motion. We would have no story without the appearance of the ghost and his stunning revelation to Hamlet that his uncle killed his father.
This revelation upends Hamlet's world and leaves him shattered: it is bad enough that his mother married his uncle with undue haste, but now a ghost, who purports to be his father, is telling him that he has been murdered by this very close relative. The ghost is also pressing Hamlet to avenge his murder.
Beyond the important of the revelation, it is also important that Hamlet receives the news from a ghost. This puts reasonable doubts into his mind as to whether he should move ahead with vengeance: what if the ghost is a spirit sent by Satan to tempt him into murdering an innocent man? If the revelation had been made some other way, such as Hamlet overhearing his uncle telling someone about the murder, or through finding a letter about the crime, his path would not have been so fraught and uncertain.
Finally, the ghost helps to establish the eerie, uneasy, corrupted tone of the play. As Horatio points out, something is wrong in the larger political body when supernatural forces like ghosts start to appear. The sense of disease and unease that Hamlet experiences is not all from his troubled psyche: Denmark really is a country out of joint, preparing for war, with spirits roaming loose and trouble in the air.