Momentum of a system is conserved in a given direction whenever the sum of the forces in that direction on the system is zero, because
(if external forces are zero, derivative of the momentum is zero, which means that momentum does not change). Remember this is a vector equation, so it applies in a given direction.
Gravity counts as an external force if it has time to affect the momentum of the system in the direction it's acting in. If gravity is acting downwards, it won't affect the horizontal momentum of the system. In a very short collision, even if gravity is acting in the direction of momentum we're considering, we usually neglect its effect on the momentum. In contrast, if you consider an object falling through some distance, gravity is changing its momentum and we don't treat momentum as being conserved.