There are a few tricks to getting IMAP on Apple Mail to behave correctly with Gmail. Rather than me spelling this information out, here’s a link to just one of the many articles which explain the steps:
Welcome! Log into your account
Recover your password
A password will be e-mailed to you.