After they were gone, an angel of the Lord suddenly appeared to Joseph in a dream, saying, "Get up! Take the child and His mother, flee to Egypt, and stay there until I tell you. For Herod is about to search for the child to destroy Him."
References for Matthew 2:13
So he got up, took the child and His mother during the night, and escaped to Egypt.
He stayed there until Herod's death, so that what was spoken by the Lord through the prophet might be fulfilled: Out of Egypt I called My Son.
References for Matthew 2:15