The primary reason for this relationship is Shakespeare's attempt to establish a foil, a narrative technique where two characters are used to highlight key features of one another, quite different features. In this case, Shakespeare has a highly ambitious and tyrannous and disloyal character in Macbeth; while in Macduff, the author creates a justified and supportive and loyal character.
Establishing the foil allows the author to explore several complex ideas of human nature through several characters, allowing Shakespeare to comment socially and politically about virtues that are important to the England of his time, especially since he has a new king, James I, a Scot himself, who takes the throne after Queen Elizabeth I. So for Shakespeare to write a play about Scotland and its aristocracy, about the sins of murder and greed and pride and their damning nature, along with the virtues of loyalty to the crown (king or queen) and fighting for a just cause to set the natural order of succession on its rightful path, is effective for his day and age.
For Macduff, his virtues are higlighted in Act 4, Scene 3, as he is having a discussion with the late Duncan's oldest son, Malcolm. Here, Shakespeare demonstrates his sense of loyalty to the Scottish crown, even if it belongs to the tyrannous Macbeth, telling Malcolm, "I am not treacherous" (IV.3.19). Macduff has even implored Malcolm to "Hold fast the mortal sword, and like good men / Bestride our down-fall'n birthddom' (IV.3.3-4). War isn't something to jump into and be impulsive about, since it creates "new widows...new orphans...new sorrows" (5). It isn't until his family is murdered does Macduff realize that a new course of action has presented itself. And he owes it to his dead wife and children to avenge their deaths, a seemingly virtuous moment for him, and a moment that justifies killing.
All this is different for Macbeth, whose entire journey to the crown begins with treachery. Macbeth has his moment of uncertainty in the first Act after his encounter with the witches, but his ambition is too strong, and the murders begin, starting with the unnatural murder of the king. The play seems to suggest that Macduff is of high virtue, and would be able to resist such an urge. Macbeth's arrogance, which largely stems from his successes on battlefield (afterall, he's a killing machine) affects his sense of virtue. He is capable of killing for the wrong reasons; he is capable of greed; he is capable of a traitorous act. So when Macduff vanquishes the tyrant himself, which is quite appropriate for Shakespeare to write in, the comment becomes the virtuous over the treacherous.