Aaron Stevens is right, but maybe I can clarify his explanation.
Consider the first second. At its beginning, the velocity is indeed 20 m/s, and at its end it is 12m/s, as you correctly wrote.
But what distance was covered during this first second ?
Since the acceleration was constant the mean velocity was the average, (20+12)/2 m/s, namely 16m/s. So the distance covered during the first second was 16 meters.
You are allowed to just take the average between beginning and end of that first second because the accelerations is constant. If the acceleration had not been constant the problem would have been different, more complicated, one would have to describe precisely how the acceleration varies with time. But this is a simple problem, constant acceleration, and you just take the average of the speed at the beginning and end of each second.
Again during the next second the average velocity was (12+4)/2 m/s = 8 m/s and the distance covered was 8 meters.
Now consider the third second. At the beginning, the velocity is 4m/s. It will take only 1/2 second for the velocity to reach 0, at which point the train stops for good. It does not keep accelerating, nor accelerating in the other direction. That would be a different problem, leading to a different solution. In this case it is clearly said that when the train stops, the problem ends. SO there is not a full third second, the process stops at the end after a total of 2 and 1/2 second.
Consider this last 1/2 second. At the beginning the velocity is 4 m/s, and at the end it is zero. So the average is 2 m/s. Since the duration of the motion is just 1/2 second, the distance covered during this last part of the motions is just 1 meter.
So 16 meters for the first second, 8 meters for the next one and 1 meter for the last part add up to the correct result of 25 meters.
There is a faster way to get the same result.
How long does the train keep moving ? At the beginning the velocity is 20 m/s ant the acceleration is -8 m/s^2, so it takes 20/8=2.5 seconds to stop (just what we obtained above, going step by step, one second plus another one plus half the third one).
What is the average velocity ? At the beginning 20 m/s and zero at the end, so 10 m/S on the average.
So average speed 10 m/s during 2.5 seconds, distance covered 25 meters.
To make the notion of average speed clearer, at the request of the OP, I will give more details. These details were first given in comments. I moved them later in this post at the suggestion of Aaron Stevens.
Consider the first second. The acceleration is constant, -8m:s^2, so the velocity decreases progressively from 20 to 12 meters per second. In your original reasoning you assumed that the train traveled 20 meters. But for that the velocity should have been 20 m/s during the full second, and drop at 12 at the very end, which is not the case. The velocity decreases progressively from 20 m/s to 12 m/s. If the velocity decrease, that is the (negative) acceleration, had been anything but constant the exact value of the covered distance would have been difficult to compute.
But there the velocity decreases at a constant rate. Consider the first and the last millisecond. During the first one the velocity is practically 20 m/s, during the last one it has practically reached the final velocity 12m/s. The distance covered during these two milliseconds is one thousandth of (20+12)=32 meters. Now take a millisecond some time T (less than a half second) after the beginning. The velocity is 20-8T because the acceleration is -8m/s^2.
Consider the millisecond that precedes the end of the first second by T. The velocity is 12+8T since it will reach 12 after the interval T at acceleration -8m/s^2. So the distance covered during those two milliseconds will be (20-8T+12+8T)=32 thousandths of a meter. So this is the distance covered over each pair of milliseconds placed symmetrically within the first second.
Add them up, remembering that when T reaches a half second you have already covered the entire first second because you are counting the milliseconds in the first half second and those on the second half together, you get 500 times 32 thousands of a second so 16 meters. It is as if you had covered the first second with a velocity that is the average between 20 and 12 m/s. This only works because the acceleration is constant.
You could equally have started by computing how long the process would take, 20 m/s divided by absolute value -8m^/s^2 of the negative acceleration shows that the trains stops after 2.5 seconds. Than consider the very first millisecond at 20m/s and the last one at extremely slow speed, distance covered one thousandth of (20+0) meters, and coupled milliseconds at time T after the beginning, velocity 20-8T, and T before the end, velocity 8T, so 20 thousandths of 20 meters for each pair. But now T extends till the half of the full duration of the process, thus half of 2.5 seconds. Always the sum of extremal velocities, multiplied by half the duration, or equivalently, full duration times the AVERAGE of the initial and final velocity of the time interval. But once more ONLY IN THE CASE OF CONSTANT ACCELERATION otherwise it does not work. –