I found the following post that identified that one possible reason for the inability to log on to iCloud and the constant spinning gear can be a missing serial number. In my case, I had a motherboard replacement and the Apple tech forgot to reinitialize the serial number. Restoring it with the software referenced below solved my problem with iCloud log-ons! For the record, I'm on El Capitan 10.11.3 on an early-2009 iMac.
Go to the Apple menu (upper left corner of your screen) and choose About This Mac. If your serial number is displayed, then this post isn't for you. If it's missing, here is now to fix it.
I want to stress that this worked for me, but you are fooling with firmware so be sure you feel comfortable with this process.
Read the page at the link below carefully before you proceed. The CD you create needs to be a bootable one, so you can't just copy the file to a blank CD.
--------------------
Previously, I was never able to login to use iMessage from my desktop Mac, but basically ignored the problem (used my iphone). After El Capitan, I was not able to access iTunes or iCloud at all.
I did notice that I was missing a serial number on my computer (from Finder, go to About This Mac). Somehow my serial number got wiped when I took my Mac in for a hard drive exchange (recalled under warranty).
This webpage showed me how to reinitialize the serial number (etched onto the footplate of the Mac chassis). It's moderate difficulty, but not impossible. The trickiest part was getting disk utility to actually burn the Blank Board Serializer to CD. After that, very simple and after restart, everything works!
http://rogersm.net/icloud-problems-mountain-lion-serial-number
Hope this helps anyone missing a serial number!