Try this simple solution, at least it works in my case.
Make sure your wireless keyboard hasn't been paired with another device before. If it has been, you have to go back to that device and unpair it. This is what happened in my case.
I previously had my Apple's wireless keyboard paired with Google Nexus 7, and it was working fine. Then I recently bought my iPad mini, and thought this BT wireless keyboard would be a perfect match for iPad mini, right? Wrong. To my surprise, no matter what I tried, the two devices failed to pair. I thought about it so hard, and I thought my keyboard is a goner. Then I went to this forum, read a few more of this discussion, but none seems to help. Then it occurred to me, perhaps I should decouple my keyboard device with previous device Google Nexus 7. Doesn't make sense though, I thought, since my Google Nexus had been off all along. Anyway, because I was desperate, I tried my way out anyway. I went to my storage place and pulled out the Nexus 7, turned it on, and it paired with my keyboard immediately. I then used Nexus 7 to unpair it with the Apple's wireless keyboard. After unpairing, I turned off my Nexus 7, and proceeded with iPad mini - wireless keyboard pairing.
At first, my iPad minin's blue tooth kept spinning and couldn't find the keyboard. Not discouraged, I held the button on the right side for 5 seconds until the green light go out to make sure they keyboard is off. Then with a flick of a second, I pushed the button to turn the device on. Immediately, iPad minin discovered my wireless keyboard. I proceeded with the pairing. iPad mini gave me a four number code. As I entered that code, the my wireless keyboard zoomed to life.
I hope this help. This is the answer for pairing iPad with Apple's Wireless Keyboard, but I believe the same principle applies to iMac as well.
Good luck!