Jesus is just a baby, but he’s already threatening the powerful people. When powerful people are threatened, they tend to react, and when those powerful people are evil, they tend to react in evil ways. That’s what happened here. Herod was visited by what was most likely a group of Persian astrologers who had seen a star after Jesus was born. They had most likely traveled a long time and had finally arrived to ask King Herod where the new king was so that they could go and worship him. This was news to Herod. After all, he thought he was the only king around.
Herod asked the chief priest about it, and his only thought was to mention the prophecies that the long-awaited Messiah was going to be born in Bethlehem. So Herod told the astrologers that they should go look for this new king in Bethlehem, but then they should come back to let him know where he is so that he too may go worship him. Like that was what he was really wanting!
Following the star once again, they found Jesus and worshiped him as the king that he was. When they left, they chose not to let Herod know where he was.
Once Herod realized that they weren’t coming back, he got angry. He reacted. He couldn’t have a new king, even if the new king was a baby. And since he didn’t know which baby was this king, he gave orders for all little boys in Bethlehem who were two and under to be killed. What an evil king. And what a tragedy.
Joseph had been warned in a dream to take Mary and Joseph and flee to Egypt.