I experienced this same issue and found what was causing it in my specific case.
While checking the Xcode-related logs in Console, I discovered that during account authentication, connections were being attempted to localhost and then rejected. This led me to check my /etc/hosts file, which contained this line:
127.0.0.1 developerservices2.apple.com
This was causing all authentication requests to be redirected to localhost instead of Apple's servers. Once I removed this line from the hosts file, I was able to log in to my Apple account in Xcode immediately.
Hope this helps!
Topic:
Developer Tools & Services
SubTopic:
Xcode