What Matthew says
"When Jesus was born in Bethlehem of Juda, in the days of king Herod, behold, there came wise men from the east to Jerusalem" (Matthew 2:1). They were looking for the newborn king of the Jews.
Herod was the king of the Jews. And he was "troubled, and all Jerusalem with him" (Matthew 2:3).
"Then Herod perceiving that he was deluded by the wise men, was exceeding angry, and sending, killed all the men children that were in Bethlehem, and in all the borders thereof, from two years old and under."
Joseph had already been warned in a dream to take the child and his mother and flee to Egypt (Matthew 2:13).
The kind of man he was
Herod the Great ruled Judea for Rome for more than thirty years. He rebuilt the Temple in Jerusalem on a grand scale. He also lived in terror of losing his throne, and he killed anyone he thought might take it.
His sister convinced him his wife Mariamne was planning to poison him, and he had her put to death. Their two sons, Alexander and Aristobulus, were found guilty of plotting against him and strangled. His eldest son, Antipater, was executed just five days before Herod's own death, around 4 BC.
A king who would kill his own wife and sons to protect his crown would not think twice about the baby boys of a small village.
Herod's pig
The joke went all the way to Rome. Around AD 400, the Roman writer Macrobius collected the sayings of the Emperor Augustus. One of them is about Herod:
"When Augustus had heard that among the children within two years of age, which Herod king of the Jews commanded to be slain in Syria, his own son had been killed, he said, It is better to be Herod's hog than his son."
The joke works because Herod, as a Jew, didn't eat pork. His pigs were safe. His sons were not.
Macrobius ties the saying to children under two killed on Herod's orders, the very age Matthew gives.
After Herod
Matthew says the holy family stayed in Egypt until Herod died. Then they came home, but when Joseph heard "that Archelaus reigned in Judea in the room of Herod, his father, he was afraid to go thither," and they settled in Galilee (Matthew 2:22).
Archelaus was indeed Herod's son, and he did take over Judea when Herod died.
Why this is a big deal
Matthew's Herod is the Herod of history. Jealous of his throne, ruthless with anyone who threatened it, and willing to kill children. The details Matthew gives, the king's fear, the age of two, the son who took over Judea after him, fit the man Rome and the Jewish historians knew.