Swords are weapons that are primarily used to kill mobs or players quicker than punching them with the bare hands. They also remove some blocks faster than using a player's bare hands (such as leaves, vines, and melons, though especially cobwebs). Each broken block counts as two uses on the sword's Durability when used in this way.
The sword is a short-range weapon. Swords were previously used to block attacks up until The Combat Update, where Shields replaced sword-blocking. A player cannot break/mine anything with this weapon.
A way to use swords without being hit is to dig a trench such that a player's head is the same level as the mob's feet. A player must of course have blocks over his head so that mobs do not come into the trench. It is then possible to hit mobs without them seeing a player: this prevents mobs from retaliating. This method is efficient when defending a fortified building, but is nearly useless when exploring except for whenever a player encounters the 2-block wide spider.