This workaround fixes many of the problems reported all over about the inconsistencies syncing Gmail IMAP with Mail.app. It keeps your inbox and sidebar nice and clean with proper mail counts. All other Gmail behaviors (trash vs. archive) remain the same.
Within Gmail.com's web interface, make the following labels (Mail.app treats labels as folders):
IMAP/Sent
IMAP/Trash
Then, within Mail.app's Accounts Preferences, on the Mailbox Behaviors tab, check/set these things:
1. Uncheck "Store draft messages on the server." (I noticed that Gmail.com erroneously keeps several incremental copies of each message and later deletes them, so I just disabled this option to avoid the problem altogether.)
2. Check "Store sent messages on the server" and select your deletion interval -- mine is set to one week.
3. Uncheck "Store junk messages on the server."
4. Check "Move deleted messages to the Trash mailbox."
5. Check "Store deleted messages on the server" and select your deletion interval -- mine is one week.
Then, on the Advanced tab, set IMAP Path Prefix: IMAP -- this relates to the labels you made earlier. Finally, in the sidebar (gacked from this post):
1. Highlight IMAP/Sent in the sidebar and select Mailbox » Use This Mailbox For » Sent.
2. Highlight IMAP/Trash in the sidebar and select Mailbox » Use This Mailbox For » Trash.
This workaround does make temporary duplicates of every message you sent and/or deleted, but they only remain duplicates for as long as you set your deletion interval above. I have not noticed any problems using this method so far, especially with correct mail counts and the possibility of deleting one message from Mail.app that deletes the whole thread in Gmail.com
Source: Macosxhints