Why is momentum conserved when a ball hits a vertical wall?

The assumption in these problems is that the collision takes place instantaneously so that gravity has no time to change the momentum of the ball during the collision.

To see why this is makes sense, let $y$ denote the vertical direction, and notice that if the collision took some small amount of time $\delta t>0$ then the change in vertical momentum of the ball would be (by integrating both sides of Newton's second law) $$ \delta p_y = \int_{t_0}^{t_0+\delta t}dt \,F(t) = F(t_0)\delta t + \mathcal O(\delta t^2) $$ so we see that as the collision time goes to zero, so does the change in momentum in the vertical direction.