Herod orders the killing of male children in Bethlehem and the surrounding region because he realizes the Magi have outwitted him Matthew 2:16. He acts based on the time he learned from the Magi about the star’s appearance Matthew 2:16.
In Matthew 2:16–18, Herod realizes the visiting magi have not returned to him and feels publicly outwitted. In anger, he orders the killing of all male children two years old and under in Bethlehem and its surrounding area, using the timing he had learned earlier. This unit completes the crisis introduced earlier in Matthew 2: Herod’s fear of a rival king and his attempt to locate the child through the magi.
Matthew presents Herod’s response as calculated violence fueled by rage when he realizes the magi did not return. The text is explicit that the order targets male children “two years old and under” in Bethlehem and nearby areas, based on the timing Herod had previously learned (verse 16).
A careful answer keeps the immediate passage, the book's argument, and the whole-Bible storyline together, so the question is answered in context instead of as a detached idea.
Start with Matthew 2:16, then read the surrounding section so the answer stays connected to the passage's flow.
16Then Herod, when he saw that he was mocked by the wise men, was exceedingly angry, and sent forth, and killed all the male children who were in Bethlehem, and in all the surrounding countryside, from two years old and under, according to the exact time which he had learned from the wise men.