How to (re)attach this baby gate to the wall?
My guess is that as the screws had come loose as they were so close to the edge of the wall with not much wall to hold them in place. Fixing the broken corner beading might not be worth it if you can cover it up with painted timber. As this beading goes all the way up the corner and if you pull it wrong, it may make it worse.
If you have access to the wall behind in the closet, try and see if you can securely add some timber so you have something solid to screw into.
Alternative Option : Have the gate at a slight angle. It looks like you can extend the sie of the gate to do this. This way you will have more that 1.5 inches to use.