How come the Sun does not make the Moon crash on the Earth? As the Moon loops around the Earth, on one side of the Earth it moves toward the Sun (let's call it downward side) and on the other side it moves away from the Sun (let's call it the upward side).

Now, I imagine the Sun's gravity attraction accelerate the Moon on the downward side and slows it on the upward side. This should raise the Moon's apoapsis on the upward side and lowers its periapsis on the downward side, gradually putting the Moon on a collision course with Earth.

Yet, this does not happen. I am curious to understand why. I suppose even a very very slim effect should build up upon billions of years, so is there a fault in my reasoning or some tidal effect correcting the Moon's trajectory?
The Sun's gravity does perturb the Moon's orbit but more subtly than you imagined.
The Moon's perigee and apogee migrate eastward in an 8.9 year cycle called apsidal precession.
Also the plane of the lunar orbit, inclined about 5.1 degrees to Earth's orbit, shifts westward in an 18.6 year cycle called nodal precession.
Lunar theory also addresses various effects on shorter timescales.

If the Moon were 4 times farther from the Earth (outside the Earth's Hill sphere), then the Sun would destabilize the lunar orbit.
Even so, the Moon would most likely drift into an independent orbit around the Sun, with little risk of collision with the Earth.