Momentum combines mass and velocity, including direction. During a collision, each cart pushes on the other with equal and opposite force for the same interaction time. Their momentum changes are equal and opposite.
If outside forces are negligible, those internal changes cancel when the whole system is considered. Total momentum before equals total momentum after.
Kinetic energy is different. In an inelastic collision, some becomes thermal energy, sound, bending, or vibration. Energy is still conserved overall, but not all remains organized as motion.