Raymond Murphy’s Grammar in Use Intermediate is one of the most comprehensive books around of grammar rules and exercises. Every student should have this. There is also Advanced Grammar in Use, and while it is very complete I think it is appropriate for an extremely high level (Cambridge Proficiency Exam). A good exercise book in between these two is Cambridge Grammar for CAE and Proficiency.
Advanced grammar guides online
The Purdue OWL (Online Writing Laboratory) is a huge reference to improve your writing. They have a large section of grammar and punctuation rules for native speakers for your most difficult grammar questions. About.com has a lot of resources although it may be difficult to find what you are looking for if you don’t know the correct terminology. Use your textbook or another grammar reference to identify what you are looking for and then...