Dan B already mentioned the Sculpt Spell metamagic feat, so I won't talk about it. There are several other possibilities.
Take a level of Archmage.
In my estimation, there is not much reason for a character who meets the prerequisites not to multiclass as an archmage. Spells per day increase as if the player had stuck to their previous arcane spellcasting class, and the class can choose from various potent abilities. In any case, among these is Mastery of Shaping, which in exchange for one 6th-level spell slot will allow the archmage to designate 5 foot cubes that are exempt from the effects of an area spell. Thus you can exclude as many allies as you want, and include as many enemies as you want. Indeed, this works quite well in combination with Sculpt Spell. In addition, any shapeable spells have a minimum dimension of 5 feet, basically meaning that a character can cast a fireball that fills only an enemy's space.
In order to take a level in archmage, a character must be able to cast 7th-level spells, among numerous other prerequisites, so this is not for all players, or low-level games.
Give your allies immunity, protection, or resistance
The spell protection from energy is 3rd level for sorcerers or wizards. It can absorb 12 points of damage per level from, say, fire: equivalent to the maximum damage from two fireballs. You can cast this on a few allies, in order to be able to cast area spells while they are brawling with enemies without much trouble. Just be sure you know ahead of time which type of energy you will be employing.
If you are willing to employ feats from the rulebook Shining South, the Selective Spell feat allows for one creature to be excluded from the effects of an area spell. One advantage of this over even Master of Shaping is that it lets you cast a fireball at grappling creatures and harm only your enemy.