Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
There can be a number of reasons for this. First of all, check if you have an internet connection (pretty obvious but worth stating). Check your WIFI and 3G/EDGE/GPRS settings. If you're on 3G/EDGE/GPRS try doing a network settings reset, which usually fixes the problems.

Also sometimes the imap.gmail servers simply timeout for a while (happens occasionally). They usually start working soon after that...
 
FWIW, I've powered off and then back on when I get that error while connecting to Yahoo mail; it's always been able to connect fine after that.
 
I have had terrible trouble connecting to my IMAP GMail on both my iPhone and MacBook Pro the last few days. I've had to use the web login since it's current not working on either system.
 
I have had terrible trouble connecting to my IMAP GMail on both my iPhone and MacBook Pro the last few days. I've had to use the web login since it's current not working on either system.

Funny since IMAP is all that has worked for me during their 2 recent webmail outages.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.