In your hypothetical, 5's hit was good enough to drive in even the # 4 guy. So there would be 2 earned runs in that inning.
What you do as a scorekeeper is reconstruct the inning in your mind as if no errors were made. And the rules specify that when in doubt, you give credit to the pitcher, i.e., questionable issues are resolved as unearned runs.
In your scenario, reconstructing would yield this:
1. Should've been out # 1
2. Should've been out # 2
3. Walked
4. Hit; now 2 on, 2 out
5. Hit that's good enough to plate 3 and 4 with 2 outs.
6. Final out. Anything that would score on his out, or that would happen in subsequent batters, would be unearned to the defensive team.
Note, however, that if you change pitchers after the initial errors, you can have situations where the run is considered earned to the reliever but not to the team. The relief pitcher individually does not get the benefit of the earlier errors. This can be a problem for those who use automated software, as it often will not permit you to have more earned runs for the pitchers than you show in your total for their team.