Can someone explain how this pawn endgame works? I'm sorry if this position has been asked before, but can someone explain how it works?

[Event "?"]
[Site "?"]
[Date "????.??.??"]
[Round "?"]
[White "?"]
[Black "?"]
[Result "*"]
[FEN "8/4k3/3p4/3P4/2P5/8/8/5K2 w - - 0 1"]

1.Kg2 Kf8 2.Kf2 Ke7 3.Kg3 Kf7 4.Kf3 Ke8 5.Kg4 Kf8 6.Kf4 Kg8 7.Ke4 Kf8 8.Kd4 Ke7 9.Kc3 Kd8 10.Kb4 Kc7 11.Ka5 Kb7 12.Kb5 Kc7 13.Ka6 Kd8 14.Kb6 Kd7 15.Kb7 Kd8 16.Kc6 Ke7 17.Kc7 Kf8 18.Kxd6 Kg7 19.Kc7 Kf8 20.d6 Kg7 21.d7 Kf8 22.d8=Q+ *


This is perfect play, but I don't understand it; can someone explain it?
I think considering an intermediate position first helps clarify what is going on. Consider the following position with Black to move. The white king is on d4, poised to head for infiltration on either the queenside or the kingside. The only drawing move for Black is to play 1...Kd8!, not committing to one side or the other. (For instance, 1...Ke7? reaches a position from the OP's main line, where White wins by heading to the other side via c3.) Playing through the variations and annotations below from this position will (hopefully) show how the opposition and distant opposition come into play rather immediately from this point.

[White "Intermediate"]
[Black "Position"]
[FEN "8/3k4/3p4/3P4/2PK4/8/8/8 b - - 0 1"]
[StartFlipped "0"]

1...Kd8 $1 
    {This is the only drawing move. When the white king comes to d4, so 
    long as the black king can play to d7 or d8, the game is still drawn. 
    But if the black king has to commit to one side or the other (to the 
    c-file or e-file) then the white king will be able to infiltrate on 
    the other side.}
    ( 1...Ke7 $2 {This reaches a position from the OP's main line, where 
    now White can infiltrate from the queenside.} 2.Kc3 $1 Kd7 3.Kb4 Kc7 
    4.Ka5 $1 {Not giving Black the chance to steal back the opposition.} 
        ( 4.Kb5 $2 Kb7 )
    )
    ( 1...Kc7 $2 {Going this direction lets White invade on the kingside.}
    2.Ke4 Kd8 
        ( 2...Kb6 $5 {White must keep in mind this counterattacking idea, 
        but it doesn't work out.} 3.Kf5 Kc5 4.Ke6 {White wins via the 
        classic trebuchet.} )
    3.Kf5 Ke7 4.Kg6 Ke8 5.Kf6 Kd7 6.Kf7 Kd8 7.Ke6 Kc7 8.Ke7 )
2.Kc3 
    ( 2.Ke4 Ke7 3.Kf5 
        ( 3.Kf4 Kf6 )
    3...Kf7 {Black maintains the peace.} )
2...Kc7 3.Kb4 
    ( 3.Kb3 Kb7 $1 {Again the only drawing move. Without maintaining the 
    distant opposition, White can infiltrate.} )
3...Kb6 
    {Black maintains the peace.}


Returning to the starting position, then, if White can maneuver his king to d4 in such a way that Black cannot play her king to the d-file in response, he will have a won game. What needs to be done to achieve that? White needs to force matters such that Black must either (1) allow the intermediate position where she can't get to the d-file, or (2) get outflanked and allow infiltration anyway. Use of the distant opposition is the only way for White to force such a decision onto Black; what complicates matters is that the mass of central pawns take squares away from each king, and that affects when one "really" has the (distant) opposition or not. The following variations and annotations attempt to show how this plays out, and to give a sense as to what considerations come into play. Certainly not every possible variation is given, but hopefully just enough wrong turns are explained to show what is going on.

[White "Starting"]
[Black "Position"]
[FEN "8/4k3/3p4/3P4/2P5/8/8/5K2 w - - 0 1"]

1.Kg2 $1 
    {This, and only this, gives Black no way to grab the distant 
    opposition. Whatever move is made now, White will be able to force an 
    untenable decision onto Black.}
    ( 1.Kf2 $2 Kf6 $1 {Maintaining the distant opposition keeps the 
    balance.} 
        ( 1...Kf7 $2 2.Kf3 $1 {Stealing away the distant opposition, the 
        only winning move.} 2...Kf6 3.Kf4 $1 {Keeping the opposition. Now 
        Black has a choice. Either allow the White king to infiltrate 
        along the outside of the kingside, or try to force the white king 
        toward the center. The latter is more stubborn, but we end up in 
        the intermediate position from above where the black king is 
        overcommitted and cannot get to the d-file after Kd4.} 3...Ke7 
            ( 3...Kg6 {Now on the g-file, there is no time to get back to 
            the d-file when the white king heads to d4.} 4.Ke4 Kf6 5.Kd4 
            Ke7 {Now we are in the OP's main line, and also the 
            intermediate position where black has had to commit to the 
            kingside and allow infiltration on the queenside.} )
        4.Kg5 Kf7 5.Kf5 Ke7 6.Kg6 Ke8 7.Kf6 Kd7 8.Kf7 Kd8 9.Ke6 Kc7 10.Ke7
        )
    2.Ke3 Ke7 $1 {Keeping the distant opposition, and not falling for an 
    appealing alternative.} 
        ( 2...Ke5 $2 {This looks like taking the opposition, but because 
        d5 is unavailable, this is just a mistake.} 3.Kd3 $1 Kf6 4.Kc3 {
        Black won't be in time to keep White out of the queenside.} )
    3.Kd4 Kd7 $1 {Kd8 works as well. This is our intermediate position 
    where Black just needs to be on the d-file.} )
    ( 1.Ke2 $2 {This also lets Black get the distant opposition.} 1...Ke8 
    $1 {The only drawing move, taking the distant opposition.} 2.Kf3 
        ( 2.Ke3 Ke7 $1 3.Kf3 
            ( 3.Kf4 Kf6 )
            ( 3.Kd4 Kd7 {Safely on the d-file.} )
        3...Kf7 $1 )
    2...Kf7 $1 3.Ke4 Ke8 4.Kd4 Kd7 {Safely on the d-file.} )
1...Kf6 2.Kf2 $1 
    {Only this, taking the distant opposition.}
2...Ke7 
    ( 2...Ke5 3.Ke3 $1 Kf6 4.Kd4 Ke7 {The intermediate position, where 
    Black hasn't stayed on the d-file.} )
    ( 2...Kf5 3.Kf3 Kf6 4.Kf4 {This is the same position as after 1.Kf2? 
    Kf7? 2.Kf3! Kf6 3.Kf4 given in another variation. Either Black gets 
    outflanked on the kingside, or allows the intermediate position 
    unfavorable.} )
3.Kg3 $1 
    {Starting to outflank, and doing so in such a way that Black cannot 
    grab the distant opposition. Only this wins.}
    ( 3.Ke3 $2 {This looks like grabbing the distant opposition, but it 
    cannot be maintained because of squares made unavailable by pawns. Now
    the game is drawn.} 3...Ke8 $1 {Only this. It prevents an unfavorable 
    instance of the intermediate position, and allows Black to grab the 
    distant opposition should White head toward the kingside.} 4.Kd4 
        ( 4.Kf4 
            ( 4.Kf3 Kf7 $1 )
        4...Kf8 $1 )
    4...Kd7 {Safely on the d-file.} )
3...Ke8 
    ( 3...Kf6 
        ( 3...Kf7 4.Kf3 $1 )
    4.Kf4 $1 )
4.Kg4 $1 
    {Again not allowing Black to grab the distant opposition, and only 
    this wins.}
4...Kf8 5.Kf4 $1 
    {Only this.}
5...Kg8 
    {Now that the black king is on the g-file, White can head for Kd4 and 
    the intermediate position where the black king can't get back to the 
    d-file.}
    ( 5...Ke7 6.Kg5 {Infiltrating as in other variations given.} 6...Kf7 
    7.Kf5 Ke7 8.Kg6 Ke8 9.Kf6 Kd7 10.Kf7 Kd8 11.Ke6 Kc7 12.Ke7 )
6.Ke4 Kf7 7.Kd4 Ke7 
    {White wins as in the OP's main line.}