Is the cam in the motor? Are you sure that the gear started to spin relative to the cam or did the whole unit rotate inside the head? The cam gear should be held in place by the pin and held down by the bolt, the two should not move together without the cam coming along. If they did, you have a serious problem. See that part on the cam where it looks like you can put a wrench? Put a wrench there and use it to hold the cam still while you break loose the bolt. Don't spare the penetrating oil. Consider heating the bolt with a torch, but be careful you don't set your penetrating oil on fire. I cannot remember if the pin is part of the cam or just set in it, but if it is integral and it is going to shear off, better now than once the motor is in the car, right? If it is integral and it has "started to shear" that means the marks on the cam gears for alignment are no longer valid, if you choose to continue using the cam. You will have to compensate. Once again, if the pin is integral you must replace the cam or the gear will snap it clean through and freewheel under load.
As for getting the gears off the cams, it is a bitch. I recommend a nice plastic nor marking deadblow hammer or a rubber mallet. While it is a pain in the rear, I would consider removing the cam from the motor (if you haven't already) and hitting all around the circumference of the gear until it comes off. If you leave it in the motor, be sure to spin it around as you hit the gear with the mallet or you will just bind it up worse.
If the pin is not integral but shears off flush with the cam, drill it out and replace it. You can get several different styles of broken bolt remover at sears which should work.