The bottom container is simply inside a well which keeps the container from moving about as it is essentially wedged in there so it cannot move from side to side or slide forward and backward, think about what holds your car battery in place, similar principal.. Prior to the top container being added small "locks" are placed on all 4 corners on top of the container on the bottom which cannot come out unless they are turned at a 90 degree angle, but before being turned into the "locked position" the 2nd container is placed on top. Then the 4 locks on are turned 90 degrees so that the 2 containers are locked together.
The same is also used on container ships. The ones loaded into the bowels of the ship are held in place by rails on the bulkhead at each end, that the containers slide into, but once the stack goes above deck level those rails stop, so they also rely on this type of "lock" that gets applied to the bottoms of the container as they are being loaded, and as they are placed on top of the container already on the ship, the machine or operator turns the locks so that it cannot move.
Of course during very rough seas it is known to have entire stacks fall of the ship as it is not a fail safe method. But within normal operations they will remain in place.