Earned run
In baseball, an earned run is any run that was fully enabled by the offensive team's production in the face of competent play from the defensive team.  Conversely, an unearned run is a run that would not have been scored without the aid of an error or a passed ball committed by the defense.  

An unearned run counts just as much as any other run for the purpose of determining the score of the game.  However, it is "unearned" in that it was, in a sense, "given away" by the defensive team. 

Both total runs and earned runs are tabulated as part of a pitcher's statistics.  However, earned runs are specially denoted because of their use in calculating a pitcher's earned run average (ERA), the number of earned runs allowed by the pitcher per nine innings pitched (i.e., averaged over a regulation game).  Thus, in effect, the pitcher is held personally accountable for earned runs, while the responsibility for unearned runs is shared with the rest of the team. 

To determine whether a run is earned, the official scorer must reconstruct the inning as it would have occurred without errors or passed balls.

Details
If no errors and no passed balls occur during the inning, all runs scored are automatically earned (assigned responsible to the pitcher(s) who allowed each runner to reach base). Also, in some cases, an error can be rendered harmless as the inning progresses. For example, a runner on first base advances to second on a passed ball and the next batter walks. Since the runner would now have been at second anyway, the passed ball no longer has any effect on the earned/unearned calculation. On the other hand, a batter/runner may make his entire circuit around the bases without the aid of an error, yet the run would be counted as unearned if an error prevented the third out from being made before he crossed the plate to score.

An error made by the pitcher in fielding at his position is counted the same as an error by any other player.

A run is counted as unearned when:

 A batter reaches base on an error (including catcher's interference) that would have retired the batter except for the error, and later scores a run in that inning by any means.
 A batter hits a foul fly ball that is dropped by a fielder for an error, extending the at-bat, and later scores. In this case, the manner in which the batter reached base becomes irrelevant.
 A baserunner remains on base or advances to the next base as the result of an error on a fielder's choice play that would put the baserunner out except for the error, and later scores.
 A batter reaches first base on a passed ball (but not a wild pitch) and later scores.
 A baserunner scores by any means after the third out would have been made except for an error other than catcher's interference.
 A batter or runner advances one or more bases on an error or passed ball (but not a wild pitch) and scores on a play that would otherwise not have provided the opportunity to score.
 Under either form of a WBSC tiebreaker in which each half-inning starts with the last one or two batters from the previous inning being placed on either second base (and if two runners, first base) to begin the inning, a run scored by these runners are unearned.  If the runners are erased on a fielder's choice which places a batter on base, and the new batter-runner later scores, this would also be an unearned run. This rule was first implemented in WBSC competitions in 2008 and in the World Baseball Classic in 2013, with Major League Baseball adding it in 2020.

While the inning is still being played, the second and the second-last scenario can cause a temporary situation where a run has already scored, but its earned/unearned status is not yet certain.  Under the last circumstance, for example, with two outs, a runner on third base scores on a passed ball.  For the time being, the run is unearned since the runner should still be at third. If the batter strikes out to end the inning, it will stay that way.  If the batter gets a base hit, which would have scored the runner anyway, the run now becomes earned.

Under the second circumstance, if there are runners on base and a batter hits a foul fly ball that is dropped, and then bats in the runners on base through a base hit (including a home run), the runs are unearned for the time being, as the runners should not have advanced. If the results of the remaining at-bats in the inning would not have scored the runners, the runs remain unearned. However, if results of subsequent at-bats would have scored the runs anyway, the runs would count as earned, unless they only would have scored as a result of a subsequent error or passed ball.

A baserunner who reaches on catcher's interference and subsequently scores with two outs scores an unearned run, but baserunners who subsequently score after the runner who has reached on catcher's interference exclusively on clean plays score earned runs; the baserunner cannot be assumed to have been put out except for the error. (2019 MLB Rule 9.16(a)(4)).

Neither the use of a pinch-runner to replace a baserunner who represents an unearned run nor the use of a pinch-hitter to continue the turn at bat of a batter who would be out except for an error transforms a run scored by such a person or his successors on base from an unearned run to an earned run.

Pitching changes
When pitchers are changed in the middle of an inning, and one or more errors have already occurred, it is possible to have a run charged as earned against a specific pitcher, but unearned to the team.  The simplest example is when the defensive team records two outs and makes an error on a play that would have been the third out.  A new pitcher comes into the game, and the next batter hits a home run.  The runner who reached on the error comes around to score, and his run is unearned to both the prior pitcher and the team.  However, the run scored by the batter is counted as earned against the relief pitcher, but unearned to the team (since there should have already been three outs).  Had the team not switched pitchers, neither run would be counted as an earned run because that pitcher should have already been out of that inning.

A pitcher who is relieved mid-inning may be charged with earned runs equal to the number of batters who reached base while he was pitching, even if the specific batters he faced do not score.  The batters he put on base may be erased by fielder's choice plays after he has been relieved by another pitcher, but if earned runs are scored in the inning the original pitcher is liable for as many earned runs as the number of batters he put on base. 
Example:
On April 15, 2017, Detroit's Justin Verlander allowed the first two Cleveland batters in the 5th inning to reach base on base hits; Verlander was then relieved by Shane Greene. Greene walked the next batter to load the bases. The next batter hit a grounder and Miguel Cabrera threw home to force out the runner on third in a fielder's choice, so the bases remained loaded with one out. Greene struck out the next batter for the second out. Carlos Santana then hit a single that scored the runners from second and third (only one of whom was put on base by Verlander), and the runner from first was thrown out at the plate to end the inning.  Since Verlander allowed two batters to reach base, he was charged with two earned runs, even though only one of the two specific batters he faced actually scored. 

When a pitching change occurs, the new pitcher is said to "inherit" any runners that are on base at the time, and if they later score, those runs are charged (earned or unearned) to the prior pitcher.  Most box scores now list inherited runners, and the number that scored, as a statistic for the relief pitcher.

See also
Baseball statistics
Earned run average
Run average

References

External links
Major League Baseball Official Rules

Baseball terminology
Pitching statistics

it:Media PGL