Masteries

Build the masteries as 9/0/21. This is the usual for heimerdingers from what I've seen.

Items

Start out with to have early game mana regen and 2 s.
If you you know you are a careless player, get 2 s.
Get as soon as you can to get your slow butt moving.
Next comes Catalyst the Protector followed by as soon as possible. is next, turning the bonus mana from RoA into ap. By this point it should be mid/late game, so get , giving you a bit of armor and ap with a very useful active.
Then get for a huge boost in ap. The last item to get can be any of the following:

- If enemy is stacking magic resistance. - If you want to play a more supportive role. - Gives your attacks huge damage after each spell use (Over 800 bonus damage late game). - Maxes out your cooldown reduction and helps those around you a bit. - If enemy is nuking you.

Rylai's slow is less effective for your grenade and is mostly useful for chasing, which you shouldn't be doing as heimerdinger. Also, the 725 mana from RoA will give an extra 21 ap with the archangel's staff. And its slightly cheaper too.

Summary

With this build, You will have over 800 ap by end game (not including elixirs), with enough health and mana to survive those late team fights. And if you got Void staff, you will have a lot of magic penetration.