What does "Alas! Poor Yorick. I knew him well," mean?
Yorick, in the scene you ask about (Act 5.1) in Shakespeare's Hamlet, was more than someone who was once nice to Hamlet, he was the court jester. He bore Hamlet on "his back a thousand times" and was kissed by Hamlet "I know not how oft."
A court jester was a clown who provided entertainment for the king and the royal household, but he was also often a friend and confidant. The role was one that allowed him to say whatever he wanted to or thought necessary to the king, without fear of reprisal. Perhaps, the jester was the only person in the kingdom who could do so.
In Hamlet, Yorick apparently spent much time with Hamlet when Hamlet was young.
Importantly, notice that the past has meaning for Hamlet here. He has changed since his opening soliloquy in which he compared the world to an unweeded garden (Act 1.2.135) and showed strong evidence that he was suffering from melancholy, or depression. All is not useless, now. The memory of Yorick has meaning for Hamlet. And Hamlet misses Yorick:
Where be your gibes now, your gambols, your songs, your flashes of merriment that were wont to set the table on a roar? Not one now to mock your own grinning? (Act 5.1.165-170)
Of course, in addition to Hamlet revealing that he misses Yorick, here, his line of thought also contributes to his past contemplations of existence, and forward to the contemplations to come:
Dost thou think Alexander looked o' this fashion i' th' earth?...And smelt so? Pah! (Act 5.1.175-78)
This is what human life comes to, for a jester or a great conqueror. But although Hamlet has not stopped thinking and contemplating, he is not made inactive by his thinking. In a minute or two, he will leap into action and leap into the grave with Ophelia, and declare:
...This is I,
Hamlet the Dane. (Act 5.1.233-34)
Hamlet has changed in more ways than one. From now on, as he says in Act 4.5.66:
My thoughts be bloody, or be nothing worth!
First of all, just to be nitpicky, there is no "well" in this speech... It goes:
Alas, poor Yorick! I knew him,
Horatio. A fellow of infinite jest, of most excellent fancy. He
hath borne me on his back a thousand times.
These lines by themselves do not particularly mean much. The lines you cite just mean exactly what they say.
But the speech as a whole is a meditation on how pointless human life is and how we all end up dead in the end. Hamlet is looking at the skull of this man who was fun and nice to Hamlet when Hamlet was a kid. Now the man is dead.
Hamlet goes on to think about various great men in history. They are all dead too -- all come to nothing in the end.
Alexander was buried, Alexander returneth into dust; the dust is
earth; of earth we make loam; and why of that loam (whereto he
was converted) might they not stop a beer barrel?
Imperious Caesar, dead and turn'd to clay,
Might stop a hole to keep the wind away.