The plot devised by King Claudius and Leartes at the end of Act 4 comes completely undone and ends in disaster for all by the end of the play. Laertes wants revenge on Hamlet for Hamlet's killing of his father, Polonius. Claudius wants Hamlet dead because Hamlet knows that Claudius murdered his brother, King Hamlet in order to gain the throne and Gertrude. They both have separate motives, but they join together to put at end to Hamlet.
The plan, simply stated, is that they will present a fencing match challenge to Hamlet. Fencing was a sport practiced by nobles and they plan to draw on Hamlet's ego to get him to want to show his skills to the court. In traditional fencing sport, the swords would have a blunted point and edge -- they are not meant to cause physical harm. The plan then is to sharpen the point to cause injury, to add a deadly poison to the sharped sword, and to put the poison in a cup of wine that would be offered to Hamlet in a toast to his good performance.
The plan is foiled because Laertes and Claudius didn't foresee any possible difficulties or "bumps along the way" in their plan. Laertes does, in fact, strike Hamlet with the sharpened sword and draws blood. Hamlet, realizing for sure that the sword fight is now more menacing and deadly, is furious and acts swiftly with his superior skills to unhand that sword from Laertes, and then he swiftly strikes back with it, causes a wound on Laertes. It is only then that he learns of the poison and that they both have mortal wounds.
In regards to the poison in the cup, Hamlet puts off the toast claiming the need to stay sharp for the match, so Gertrude takes the cup and drinks a toast to Hamlet's success. When Gertrude dies, and reports that she was poisoned, Hamlet puts it all together and realizes that Claudius is ultimately to blame. He takes the poisoned sword to stab him and dumps the poisoned cup down his throat. He knows it is too late to save himself, but be does end his quest for vengeance.
As Horatio says after all of this has happened: "in this upshot, purposes mistook / Fall'n on the inventor's heads." He is commenting on the fact that the inventors of this plot with poision end up being killed by that poision. It seems a kind of poetic justice in the end.