Applying a closure element to the mouths of bags