Why does Medea think it is necessary to kill her sons to get revenge on Jason?
Medea kills the children to spite Jason. The core focus of the play is on the fact that Jason is going to marry a Greek princess, and Medea, who is a foreigner, will be relegated to “mistress” status—her children by Jason would presumably also be illegitimate once the new marriage is contracted, although this is not stated outright. Medea is furious because she helped Jason significantly during his travails with the Golden Fleece, but Jason's only response is that he has to marry a Greek woman, so unfortunately this cannot be Medea. She replies that she’s left everything—family, homeland, language, gods—to be with Jason, and he’s casting her aside without ceremony. Jason effectively tells her to calm down and get over it.
In response, then, Medea feels she can only punish Jason properly by taking everything away from him in order to impress upon him her feeling that everything has been taken from her. She achieves this by poisoning his new wife, murdering his children (whom he presumably was going to disinherit in favor of his new legitimate children), and then running away to Athens to marry the king there. She takes the children’s bodies with her so that Jason can’t even hold them or bury them, and she makes a point of saying she’ll bury them by Hera’s temple—Hera being the ultimate representation of the wronged wife in Greek mythology. She didn’t want to kill the children, but she wanted to make the point that she lost everything to be with Jason, and since he no longer cares, she’ll rip everything away from him.
Medea would have done anything to be with Jason. These aren't just words; she backed up her passionate love for Jason with action——namely killing her own brother Absyrtus before she fled Colchis with her beloved. But now that he's betrayed her, gone off to marry Glauce, she will do anything for revenge, even if it involves killing her own children. The intensity of Medea's love is matched only by her hatred towards the man she once adored.
Medea's actions could plausibly be justified on the grounds that she's an instrument of divine will. Her falling in love with Jason was the work of Aphrodite or Eros, so Medea, as with so many mortals in ancient Greek myth, is a pawn in a much bigger game being played by the gods. Medea's killing of her children, though utterly reprehensible from the standpoint of human morality, does appear to be sanctioned by the immortals, and Medea knows all too well what happens to those proud or foolish enough to defy the gods. Indeed, one could argue that it is Jason who has offended the gods by dumping Medea and entering into an unfaithful relationship for purely political advantage.
This impression is confirmed by the deus ex machina that helps Medea to evade justice at the end. The chariot of the sun-god Helios swoops down, whisking Medea away along with her children's dead bodies. We may look askance at Medea's despicable acts, and not just in relation to her children, but the gods clearly smile upon her. And ultimately, for the ancient Greeks, that's what really matters.
I think that this particular action is where Medea ends up moving into a realm where her actions are indefensible. In her mind, she sees it necessary to kill Jason's sons for a couple of reasons. One of the reasons she uses to justify killing the children is because of pragmatism. Medea figures that "the Corinthians will kill the children anyway, in retaliation for her murder of Creusa and Creon." This aspect of practicality in Medea is one reason she uses for her actions. Another reason is out of pure spite and wrath towards Jason. When Jason indicates to her that she will suffer as well, Medea argues that this experience is secondary to her being able to take from Jason. The anger that is felt towards Jason compels her to kill the children. In another respect, Medea represents the idea that the emotional experience of jealousy and vengeance can be all encompassing, one that knows no limitations. Civil society would regard the killing of children as one of the worst crimes and a point from which there can be no return. Yet, Medea's jealousy and rage, her feelings of hurt caused by Jason's action, causes her to be irredeemable and past that point, demonstrated by the killing of her own children.