Hacking a turret requires a Specialist Sergeant specced out to include the "Haywire" ability. Turrets can be found quite early in any campaign at random, but are a guaranteed part of the Blacksite defences so that's probably the first place you'll encounter one.
Haywire will use your Specialists hacking stat to oppose the turrets hack defence of 70 - the higher your stat the better your chance of hacking and dominating it. The Hacking stat will increase with rank but you can also give it a boost by researching and building improved GREMLINs (requires the Advent MEC Breakdown research project).
Once you've found your target, save the game, then try to hack it. If it doesn't work, load the save and try again until it does.
The hard part is actually killing anything with the turret once you have it as they seem to have terrible aim. Your best bet is to try softening up any viable targets with your other troopers and hoping for the best. Again, save scumming is probably a good idea.
Alternatively, if you have the "Shen's Last Gift" DLC pack you can this done easier and earlier than it could be completed in your vanilla campaign.
During the "Lost Tower" mission, Lily Shen will be part of your ground team and if you attempt it as soon as it's available in your campaign she will be significantly better than any other support trooper you have available (she seems to be equivalent to a Colonel but with access to all skills, and a hacking stat of 140). Once you progress to the second segment of the mission, you'll find yourself in a large hanger type room. Moving towards the objective (the "Spark") will activate a number of dormant Decaying Turrets. Simply use Lily's Haywire ability to take one of them over (it should be close a 100% success rate) and use it to attack any other turret in range.
Again, they are awful shots however so you may want to soften up the enemy turret just to make sure.
While on this mission it is also possible to get the following achievements the same relative ease:
The Cyberlord achievement in XCOM 2 worth 14 pointsEarn a second tier hack reward (Taking control of the turret will also count for this)