I used to play a ton of Gragas mid, before Riot changed him, and since then I've only played him as support. Riot's changes intended to make him more of a fighter, but honestly, you play him pretty similarly as you did pre-change (only now you don't spam your W every chance you get, especially since it has a mana cost now). For the most part, you max Q, and poke with your barrels (double cast Q so it explodes immediately when it arrives, unless you need them to waveclear or something, in which case leave it and it'll gain more damage as it sits. You'll know once it turns red that it's at max damage). What you generally do is W before you want to go in, and use your E as a gap close/stun on the enemy, and auto them with the empowered W. Another thing you can do is sort of similar to Lux, where you use W, launch your Q (in Lux's case, her E) at your opponent and immediately body slam them afterwards, so they're stunned, use your W auto, and detonate your Q. This way, Q has more time to ramp up in damage. Also, his Q gives vision, which can help in certain situations.
His ult you use either for burst and/or for it's displacement effect, which makes it honestly one of the most powerful teamfight ults; it lets you spread their team out so your team can pick people off. Placing it is vital, but it has a ridiculous amount of utility since it can launch their carries towards your team, launch their front line away from your team, shove the enemy into walls to stun them, etc. Basically, it's either used at the beginning of a fight to move someone into a favorable position (ie. into your team, into your turret, etc.), or at the end of a fight as a finisher/execute (usually the former, but you can use it as the latter as something of a last resort).
As a Support, I'll usually build him pretty tanky, where I use him mostly for his utility. I usually take 0/16/14 masteries on him, and I will try to build more HP than raw resistances (face of the mountain, sightstone, banshees/locket), but I will also go for frozen heart for armor, mana, and CDR. I also like mobility boots on Gragas, since he has great potential for engaging, and this helps you get into position. Usually, for damage, I only build a Haunting Guise, since it gives more HP, good magic pen, and AP, which lets you do respectable damage. Basically unless I need to provide tons of damage, I'll go tanky. HP works really well on Gragas since his passive restores %HP, so if you have more max HP, it'll restore more health. Also, his W gives you some resistances, so HP works well. In lane you're basically playing like Karma; poking and zoning with your Q. You have more potential for hard engage than Karma because you have an AoE stun in your E, and also once you get your ult, you can really separate a carry from the rest of their team, but I typically am only poking and zoning to give my ADC more breathing room. Honestly, though, I think I should be building more damage on Gragas, since he's really not a pure tank; I would probably also add a Sheen to my build.
I haven't played Gragas much in top lane, but I know you're going to want to poke and zone with your Q, and just farm more than anything. You're a lane bully, and you can really use your Q to mess with people. Basically, you poke until you can all in; you can use your E and W to trade, but you're really not a fighter champion like Master Yi or Tryndamere or something; you favor short, bursty trades (and poke mostly). You're actually not going to want to go too tanky as top lane Gragas, since he's a champion that's not really meant to be a tank. You usually want to go sheen first, and then either go straight into Rod of Ages if you're a little behind and need the health more, or if you can, get Lich Bane finished. From there you basically build him like a beefier mage; things like Zhonyas or Void Staff work well, Sorc Shoes or Lucidity boots (if you don't get something like Morellonomicon for CDR) for boots unless you need something like Merc Treads or Ninja Tabi.
Hope this helps