Momentum is mass multiplied by velocity, so it has a direction as well as a size. In a collision between objects with no outside forces acting, the total momentum before the collision is equal to the total momentum after it. This is the conservation of momentum.
In an inelastic collision the two objects stick together. Suppose a 2 kg trolley moving at 3 m/s hits a stationary 1 kg trolley and they join. The total momentum before is 2 × 3 = 6 kg m/s, so the total momentum after is also 6 kg m/s. The joined trolleys have a mass of 3 kg, so their speed is 6 ÷ 3 = 2 m/s. In a near elastic collision, such as trolleys with magnets or springs, the objects bounce apart but the total momentum is still the same.
In an explosion or recoil, objects start at rest so the total momentum is zero. When a gun is fired, the bullet gains momentum forwards and the gun gains the same amount of momentum backwards, so the total stays at zero. This works because of Newton's third law: the two objects push on each other with forces that are equal and opposite and act for the same time, so one gains as much momentum as the other loses.