Hamlet was never very close with these two, and accuses them immediately: "I know the good king and queen have sent for you." Unlike his real friend Horatio, they have no reason to come to the hinterlands of Denmark other than to spy for Claudius.
The two come and go with the king and queen to the play which Hamlet stages, and then they bring the message from Gertrude that she wants to see him after it. It's pretty obvious that they are working for Claudius at least, and Hamlet knows Claudius killed his father. So they share in his evil to some degree.
He finally accuses them of lying to him and trying to manipulate him, like playing a recorder:
HAMLET: 'Tis as easy as lying: govern these ventages with
your lingers and thumb, give it breath with your
mouth, and it will discourse most eloquent music.
Look you, these are the stops.
GUILDENSTERN: But these cannot I command to any utterance of
harmony; I have not the skill.
HAMLET: Why, look you now, how unworthy a thing you make of
me! You would play upon me; you would seem to know
my stops; you would pluck out the heart of my
mystery; you would sound me from my lowest note to
the top of my compass: and there is much music,
excellent voice, in this little organ; yet cannot
you make it speak. 'Sblood, do you think I am
easier to be played on than a pipe? Call me what
instrument you will, though you can fret me, yet you
cannot play upon me.
He says it again after he kills Polonius, and Claudius sends them to him to find Polonius's body:
ROSENCRANTZ: Take you me for a sponge, my lord?
HAMLET: Ay, sir, that soaks up the king's countenance, his
rewards, his authorities. But such officers do the
king best service in the end: he keeps them, like
an ape, in the corner of his jaw; first mouthed, to
be last swallowed: when he needs what you have
gleaned, it is but squeezing you, and, sponge, you
shall be dry again.
Claudius sends Hamlet off with the two, and on "a plain in Denmark" they run into Prince Fortinbras of Norway and some of his soldiers. Fortinbras is attacking a little chunk of Poland purely because it's "supposed" to belong to Norway ("Truly to speak, and with no addition,/ We go to gain a little patch of ground/ That hath in it no profit but the name.") and the captain of the group tells Hamlet that 20,000 men may die in the fight.
Hamlet feels prodded morally by the willingness of twenty thousand men to die for a useless patch of ground, and realizes that he has to go back to Denmark and avenge his father's murder: "O, from this time forth,/My thoughts be bloody, or be nothing worth!"
So he boards the ship with Rosencrantz and Guildenstern in this frame of mind. When he secretly reads the letter signed by Claudius which orders his execution, he doesn't know if R&G are in on the execution plot, but he knows that they are working for the king. He has to escape one way or another. It's not pleasant, but he figures that if they're going to throw in their lot with an incestuous murderer, they deserve whatever they get. So he rewrites the letter to send them to their deaths.
It wasn't that they showed up and innocently accompanied him on the ship; they've been actively reporting on him to Claudius for a chunk of the play, and trying to manipulate him for the king's nefarious ends, and Hamlet knows it.