I think that when Hamlet decides to fight Laertes, he does realize that he might not come out of the fight alive. Osric tells him that Laertes is very good with his weapon, and Hamlet replies that he, too, is good with a weapon and has been practicing, so people should not assume he will lose the fight. Later in the coversation, however, he says:
Not a chance. We defy omens. There's a special divine
intervention in the fall of a sparrow. If it’s now, it’s not to
come. If it’s not to come, it will be now. If it’s not now, it
will still come. Being ready is everything. Since no man
has anything of what he leaves, what is it to leave soon?
This indicates that he is still rather depressed and doesn't really care if he dies. He means that everyone has to die, so what difference does it make whether it is sooner or later?
Claudius has bet on Hamlet to win the fight, but he has secretly poisoned the wine with a poisoned pearl. He has also poisoned Laertes' sword. Gertrude mistakenly drinks from the poisoned cup and dies. Laertes wounds Hamlet with the poisoned sword, but then they accidently switch swords and Hamlet wounds Laertes with the same poisoned sword.
Laertes reveals the conspiracy. Hamlet stabs the King and forces him to drink poison. Laertes and Hamlet reconcile. Laertes dies. Hamlet stops Horatio from drinking the poison so that he can live to tell the truth. Hamlet names Fortinbras his successor. At the end, Fortinbras arrives and orders Hamlet to be buried with full honors.
Read more here on enotes, where you can also read the full text with the modern English translation - very cool.