In Exodus when the firstborn sons of all Egyptians die because of Pharaoh, why did God kill all the Egyptian sons for the sin of Pharaoh?
It is easy to blame the leader and forget he wasn't the only one involved. He was by far the most prominent, but he wasn't working alone.
God told Abraham, "Know certainly that your descendants will be strangers in a land that is not theirs, and will serve them, and they will afflict them four hundred years" (Genesis 15:13). This came about simply because the Israelites became numerous while living in the Egypt. "Now there arose a new king over Egypt, who did not know Joseph. And he said to his people, "Look, the people of the children of Israel are more and mightier than we; come, let us deal shrewdly with them, lest they multiply, and it happen, in the event of war, that they also join our enemies and fight against us, and so go up out of the land" (Exodus 1:8-10). Notice that this Pharaoh feared that the Israelites might join an enemy nation. There was no evidence of that, but his ideas were adopted by the Egyptians. "And they made their lives bitter with hard bondage-in mortar, in brick, and in all manner of service in the field. All their service in which they made them serve was with rigor" (Exodus 1:14). Worse, they turned their attacks on babies. "This man dealt treacherously with our people, and oppressed our forefathers, making them expose their babies, so that they might not live" (Acts 7:19).
After 400 years of this, God stepped in. "And the LORD said: "I have surely seen the oppression of My people who are in Egypt, and have heard their cry because of their taskmasters, for I know their sorrows"" (Exodus 3:7). Remember that Pharaoh was leading his people, but it was the Egyptians who were carrying out Pharaoh's plans.
Egypt was a country of idolatry. Each of the plagues showed God to be greater than the Egyptian gods. "For I will pass through the land of Egypt on that night, and will strike all the firstborn in the land of Egypt, both man and beast; and against all the gods of Egypt I will execute judgment: I am the LORD" (Exodus 12:12). David Padfield wrote, "This plague was directed against "all of the gods of Egypt" (Exodus 12:12) and would show the total inability of the gods of Egypt to protect their subjects. In the face of unparalleled tragedy, "all of the gods of Egypt" were silent. Where was Meskhenet, the goddess who presided at the birth of children? Where was Hathor, one of the seven deities who attended the birth of children? Where was Min, the god of procreation? Where was Isis, the goddess of fertility? Where was Selket, the guardian of life? Where was Renenutet, the cobra-goddess and guardian of Pharaoh?" [Against All The Gods Of Egypt]. You need to also know that the title "Pharaoh" means "the sun." All the kings of Egypt believed they were deity on earth. Pharaoh was powerless to do anything to stop God. This was proven not to just Pharaoh, but to every Egyptian and to everyone in the world.
Thus God took a cruel, arrogant, corrupt people and brought them to their knees. He also punished them by bringing upon them what they did to others -- killing babies -- though, with God all children would be brought to Him in heaven because of their innocence.