(This is my first post in more than five years... I was an active user on these forums).
I spent an hour reading this entire thread to address my similar problem. The main problem I had was the darn verification code thing. On the Mac, there is no field for the verification code and while the instructions provided on the pop up screen on other trusted devices says to enter the verification code right after the password, it wasn't working for me.
Until I studied this entire thread.
My problems centered with logging in with my Apple ID but spanned through the iTunes Store, App Store, and iCloud, all on my Mac. By the way, I'm using an 11-inch MacBook Air (Mid-2011) running Yosemite 10.10.5.
Here's what I did and my experience:
0.First I went to System Preferences to configure iCloud. Someone on this thread said to log out and log back in. Apparently I was never signed in to iCloud to begin with on my Mac because of the failed login issue. So I attempted to login to iCloud with my Apple ID and password and you know what happened? A new dialog box popped up with a field to enter the verification code! Upon doing that, I was able to complete iCloud login successfully.
0.Second, I proceeded to iTunes so that I could complete my business there. I was trying to access my account but could not because you know why. Previously I was unsuccessful. But after doing the previous step, and then logging in, I followed the directions provided by someone on this thread which was to first input your User ID and password,, click log in, and wait for the verification code on your other trusted device. When you get the code, go back to your Mac and delete the password you just put in and re-type the password followed by the verification code after it, with no spaces and on the same field as stated in this thread. There was no pop op box for a verification code like with iCloud setup, but it worked this time and I was able to finally get into my iTunes account.
0.Finally, the App Store where I had been trying to download OS Sierra to upgrade my software. Of course, I wasn't able to do this because of the Apple ID verification issue. But because I had done my step one of going to System Preferences to setup iCloud first, I was now able to successfully log in to the App Store following the procedures in step two above.
Sadly, while the solution worked initially, I am back to square one for some reason. I have DSL so downloading OS Sierra at approximately 5GB takes a long time. I left it downloading overnight to find in the morning that it had only progressed 34%. When I came home in the evening to check on the status, I found the download had stopped due to an error. I tried to resume the download and got a"-1004" error notice (which I looked up elsewhere on the Apple Support Communities for a solution but I decided to not bother with it) so I shut down my computer and restarted. Going back to the App Store, I checked for purchased downloads but did not find OS Sierra. I did find it in the updates list as a free upgrade and clicking on the corresponding button prompted me to log in with my Apple ID. I did the trick of deleting the password and re-entering the password followed by the verification code but to no avail.
So? I'm back, in a sense, to where I started! (I even tried to log out of iCloud and start over to reset the successful process I previously had done. I got iCloud up and running again but no App Store access. I didn't check iTunes because I wanted to leave it alone.).
The mystery continues...
I contacted Apple directly via email to share my issue. If they provide a solution not shared here, I will post back.