To be more specific on how the ejection port (dust) cover operates, if you examine the dust cover when it is open, you'll notice that there is a small bump at the middle of the edge of the cover. Inside this bump houses the spring loaded retaining detent the keeps the cover closed when it is snapped into the upper receiver. Since the detent is spherical in shape and spring loaded it is easily pushed in and out of its housing as the cover is snapped closed and open.
Notice also that when the bolt is fully forward home or out of the gun that there is a recess ground into the side of the bolt carrier where the cover's detent housing would normally go into when the cover is closed. With the bolt fully home or lock in the rear position, the detent housing has room to allow the cover to be snapped close. The recess of the bolt carrier is rounded at the ends and the detent housing is also round.
With the cover closed nd the bolt home, when the bolt travels rearward either manually or under recoil during firing, the recess moves back and the detent housing gets pushed out by the bolt carrier recess's edge opening the cover automatically. With the bolt locked in the rear position and the cover closed, releasing the bolt will send the front edge of the bolt carrier into the detent housing's round surface pushing it out opening the cover. In either case, when the bolt (carrier) is moving, the cover will get pushed open if it is closed.