iMac keyboard stops working
Do you have another Mac you can connect the keyboard to? If you can isolate whether or not this issue occurs only with your iMac or only with your iMacs keyboard, you can figure out where things are going wrong. Let us know if you're able to test the keyboard on another Mac or test the Mac with a different keyboard. 

If you don't have access to either of those things, I'd recommend taking a trip to your local Apple Store and having them test it out. They'll have extra Macs and keyboard and a Bluetooth diagnostic to run as well.