Hamlet and Horatio are friends. They were fellow students at Wittenberg. Their friendship develops throughout the play because Hamlet finds he can trust Horatio and confide in him. And Hamlet desperately needs a friend.
Shakespeare invented the character of Horatio because he needed to have someone for Hamlet to confide in. Most information in a play is communicated by dialogue. Hamlet reveals some of his thoughts and feelings in his many soliloquies. But Shakespeare must have felt that he was risking becoming tedious with too many soliloquies. Such speeches are unnatural anyway. People do not normally express their private thoughts and feelings as if they are talking to themselves. The creation of Horatio enabled Shakespeare to advance his plot and convey information to his audience through ordinary dialogue. A good example of this is the scene in which Hamlet and Horatio are preparing to keep careful watch on Claudius during the play within a play.
There is a play tonight before the King.
One scene of it comes near the circumstance,
Which I have told thee, of my father's death.
I prithee, when thou seest that act afoot,
Even with the very comment of thy soul
Observe my uncle. If his occulted guilt
Do not itself unkennel in one speech,
It is a damned ghost that we have seen,
And my imaginations are as foul
As Vulcan's stithy. Give him heedful note;
For I mine eyes will rivet to his face,
And after we will both our judgments join
In censure of his seeming. (3.2)
The negotiations with the pirates who have captured Hamlet at sea would have been nearly impossible without Horatio. He makes it possible to rescue Hamlet without Claudius hearing about it. Throughout the play Horatio acts as Hamlet's confidant, and in doing so he makes it easy for the audience to follow the plot. Horatio proposed to commit suicide so that he could join his friend in death. Hamlet discovered that he couldn't trust another soul at court, not even Ophelia, the girl he loved. His other school friends Rosencrantz and Guildenstern were treacherous and self-serving. Only Horatio is a true friend, and such true friends are rare.
Hamlet is a prince. He has hangers-on and courtiers fawning over him. He's a powerful celebrity surrounded by flattery. The scene with Osric is a perfect example. Even good friends like Rosencrantz and Guildernstern fail to remain true when dazzled by the king's offer of rewards. Hamlet's royalty makes it hard for him to have genuine friends.
But Horatio is not a sychophant. He is a friend from University, not a courtier or from an important family. Hamlet says this to Horatio about their friendship:
Nay, do not think I flatter;
For what advancement may I hope from thee
That no revenue hast but thy good spirits,
To feed and clothe thee? Why should the poor be flatter'd?
No, let the candied tongue lick absurd pomp,
And crook the pregnant hinges of the knee
Where thrift may follow fawning. Dost thou hear?
Since my dear soul was mistress of her choice
And could of men distinguish, her election
Hath seal'd thee for herself; for thou hast been
As one, in suffering all, that suffers nothing,
A man that fortune's buffets and rewards
Hast ta'en with equal thanks: and blest are those
Whose blood and judgment are so well commingled,
That they are not a pipe for fortune's finger
To sound what stop she please. Give me that man
That is not passion's slave, and I will wear him
In my heart's core, ay, in my heart of heart,
As I do thee.