A turret is an automatic defensive mechanism, programmed to shoot anyone who is not being followed by the player who placed it. Most dungeons contain turrets -- these turrets will fire on everyone.
How to Obtain
You can loot turrets of all types, craft them, or trade for them in market worlds. You can also occasionally receive a turret by mining one from a dungeon (although usually you'll get scrap metal). If you destroy a player-placed turret (including one of your own), you'll get the turret.
How to Use
Use turrets when creating a challenge build, or to ward off non-friends from your other creations.
Turrets can be destroyed either by mining them or by shooting at them.
Note that turrets must be placed at least 25 blocks from spawns, and those placed in market worlds will not function.
Functioning turrets were added in June 2012.
In November 2014, turrets in market worlds were made non-functional as part of the 1.21.0 release. This was in hopes of cutting down on trolling in market worlds.