It's a great question, and one of the biggest problems in the play. Why does Macbeth kill Duncan when Duncan has two sons who would legally just succeed their father's throne? It doesn't make any sense. But the reason Macbeth does it is quite simple: because of the witches' prophecy in his first scene:
All hail, Macbeth, who shalt be king hereafter.
Though it doesn't quite make absolute sense, Shakespeare handles it by having Malcolm and Donalbain, Duncan's two sons, run away to England and Ireland respectively - and everyone, we learn later from Macduff, suspects them of their father's murder. By some process or other (unexplained by the play) Macbeth then ascends the throne.
Yet the heir to the throne is still a problem. Macbeth knows that the witches predicted that Banquo would "get kings" (beget kings - father kings) though Banquo himself would never be a king. Therefore, when Macbeth is the king, he needs to secure his throne - he is terrified that Banquo's children will steal his throne. Yet Lady Macbeth and Macbeth are childless:
Our fears in Banquo
They hail'd him father to a line of kings:
Upon my head they placed a fruitless crown
And put a barren sceptre in my gripe,
Thence to be wrench'd with an unlineal hand,
No son of mine succeeding.
Macbeth is aware that he needs to remove the competition: but also that he needs to solve the succession problem - a king with a son means a stable country. A king with no heir - doesn't.
In (4.1) MacBeth demands that the witches give him reassurance that he is heir to the throne. The witches show him a series of apparitions which he takes literally and is assured that he is heir to the throne. The third apparition is a crowned child, holding a tree in his hand. The child urges MacBeth to "be lion mettled, proud, and take no care / who chafes, who frets, or where the conspirers are" since he is safe until "Great Birnam wood to high Dunsinane Hill / Shall come against him" (4.1.103-7). MacBeth takes this literally and thinks he will remain heir. Unfortunately, the last vision of the line of Kings with Banquo at the end stirs him to greater bloodshed and mayhem. It is after this where MacBeth orders the slaughter of MacDuff's wife and children. Act four doesn't make MacBeth think he should be heir, but it shows how the dtate of MacBeth's confidence and how he is actively trying to protect his throne.