I'm pretty sure what you want is to avoid the grapple in the first place. That's really hard because, for the most part, it's just a melee touch attack to start. So unless you're immune to being touched (possible but difficult) if you're immune to grapples before you know someone's going to grapple you, you're going to end up immune to attacks in general.
Assuming you don't want to play that game (the one, wherein, y'know, you're invulnerable to the mundane), escaping from a grapple is fairly easy, and, while harder, not getting grappled if you know it's coming is better. Here are ways of doing both. Omitted are sources providing effects under specific circumstances (e.g. in webs, underwater). The spells freedom of movement and heart of water included for completeness.
Spells
Best
- The 3rd-level runescarred berserker (Una 31-2) (minimum caster level 5) spell freedom of movement [abjur] (PH 233) for 10 min./level grants 1 creature the ability “to move an attack normally… even under the influence of… paralysis, solid fog, slow, and web,” to automatically succeed on grapple checks to resist grapple attempts and on grapple checks and Escape Artist skill checks made to escape a grapple or pin, and to move and attack normally underwater.
- The 3rd-level Sor/Wiz spell heart of water [trans] (CM 107) for 1 hour/level grants the caster a +5 enhancement bonus to Escape Artist skill checks, the ability to breathe water, and a swim speed equal to the caster’s land speed; as a swift action that otherwise ends the spell the caster gains the benefits the spell freedom of movement [abjur] (PH 233) for 1 round/level.
Other Sources by Level
Many, many spells grant bonuses to grapple checks. If a creature can get a grapple check bonus high enough to just win the opposed check, he can end the grapple. I've not listed those spells. Many spells also grant a bonus to Escape Artist skill checks. I've not listed those either. I find the 2nd-level Sor/Wiz spell unfettered grasp [ench] (RDr 118) too situational to include.
- The 2nd-level Sor/Wiz spell mountain stance [trans] (SpC 144-5) for 1 min./level grants 1 creature the ability as a free action to gain a bonus equal to the caster's level versus "attempts to grapple, lift, push, bull rush, overrun, throw, trip, or otherwise force the subject to move." Notable for the duration, lack of a caster level cap, untyped bonus, and versatility.
- The 3rd-level Sor/Wiz spell hesitate [ench] (PH2 114) as an immediate action for 1 round/level on 1 creature's failed Willpower saving throw the creature takes only 1 move action; the creature makes another saving throw each turn to end the effect. Notable for the casting time.
- The 3rd-level Sor/Wiz spell permeable form [trans] (LoM 129) as an immediate action for 1 round grants the caster the benefits of the incorporeal subtype (MM 310-1).
- The 8th-level Sor/Wiz spell ghostform [trans] (SpC 103) for 1 round/level grants the caster the benefits of the incorporeal subtype (MM 310-1).
Magic Items
Best
- The face slot item third eye freedom (MIC 141) (2,400 gp; 0 lbs.) as an immediate action 1/day grants for 1 round an effect like the spell freedom of movement [abjur] (PH 233).
- The shoulders slot item shadow cloak (DrU 101) (5,500 gp; 1 lb.) as an immediate action 3/day grants the wearer the ability to teleport up to 10 ft.
Other Sources by Price
- The armor and shield augment crystal crystal of glancing blows (least) (MIC 25) (500 gp; 0 lbs.) grants a +5 competence bonus to opposed grapple checks when the wearer's the defender and a grapple's initiated.
- The slotless item panic button (escaping) (CS 115) (750 gp; 0 lbs.) as a swift action once only grants an effect like the spell dimension door [conj] (PH 221) with a 30 ft. range that "affects whatever creature is in the square it lands in," never accounting for creatures sharing squares like during a grapple. Item goes unmentioned in errata. Check with the DM.
- The feet slot item anklet of translocation (MIC 71) (1,400 gp; 0 lbs.) as a swift action 2/day grants the wearer the ability to teleport up to 10 ft.
- The belt slot item silkslick belt (MIC 136) (2,000 gp; 0 lbs.) grants the wearer the ability to take a move action to make an Escape Artist skill check to free himself from a grapple or pin, and as a swift action 1/day it grants the wearer a +10 competence bonus to 1 Escape Artist skill check made before the wearer's next turn ends.
- The torso slot item vestment of ebonsilk (MIC 147) (5,000 gp; 1 lb.) grants an ongoing effect like the spell freedom of movement [abjur] (PH 233) to characters meeting specific requirements. Controversy: You can "establish the proper divine connection" by making 2-3 Use Magic Device skill checks. First, if needed, emulate an alignment (PH 85), picking CE, NE, or CN (DC 30); second, emulate the class feature (PH 86) Deity, Domains, and Domain Spells (PH 32) to pick Lolth as a deity like a 1st-level cleric (DC 21); third, emulate the class feature Spells (PH 32) to gain the spellcasting of a 13th-level cleric who has an open 7th-level spell slot (DC 33) that's sacrified to activate the relic power. Check with the DM; objections to part or all of this chain of events are common as the Use Magic Device skill is an interpretive nightmare.
- The torso slot item vest of free movement (MIC 198) (12,000 gp; 0 lbs.) as a swift action 3/day grants for 3 rounds an effect like the spell freedom of movement [abjur] (PH 233).
- The hands slot item scholar shadow hands (ToB 150) (15,000 gp; 0 lbs.) can grant a HD 18 creature the 5th-level Shadow Hand maneuver shadow stride [special] (ToB 80).
- The ring slot item ring of freedom of movement (DMG 231, 232) (40,000 gp; 0 lbs.) grants an ongoing effect like the spell freedom of movement [abjur] (PH 233).
- The armor special ability freedom (MIC 11) (+5 bonus) grants an ongoing effect like the spell freedom of movement [abjur] (PH 233).
Feats
There's only 1 feat you should barely consider if this is happening all the time, and that's the feat Close-quarters Fighting (CW 97). It's one feat, you'll meet the requirements, and if you've a spiked gauntlet (so you're threatening an area) you can make an attack of opportunity versus a creature that tries a grapple even if it has the special ability improved grab or the feat Improved Grapple (PH 95-6). Hitting it either stops the grapple cold or give you a bonus. You won't hit. You're a wizard. But 5% remains 5% and + something remains better than + nothing. Don't do this, though.
Your cohort, homunculus, intelligent animated dead, planar bound minion, or whatever, however, should take the feat Dutiful Guardian (DrU 50). Then you can rescue him (or not) while he's grappled instead.
Finally, if you're level 18 you can just take the feat Martial Study (ToB 30-1) and take the 5th-level Shadow Hand maneuver shadow stride [special] (ToB 80). Further, it has definite advantages as an extraordinary (rather than spell-like or supernatural) teleport ability. You can take the feat Martial Study at level 6 instead if an extraordinary teleport as a standard action is sufficient by picking the 2nd-level Shadow Hand maneuver shadow jaunt [special] (ToB 79)