The rapier developed at the very end of the 16th century as a modification of what is now known as the "cut-and-thrust" style sword. It was slimmer and nimbler than the thrusting broad-sword, a feature that enabled it to grow longer, and increased the usefulness of thrusting attacks thanks to its reach.
The rapier became popular in Europe in the 16th century primarily as a weapon for civilian use. In parallel to the rapier, other weapons were developed for use in war in response to the increasing protection offered by fully articulated plate armourestoc and many specialty polearms-- such as the later versions of halberd.
As steel-plate armour became obsolete or shrunk in size due to the increasing use of firearms in the late 16th century and the beginning of the 17th century, the rapier, in various modified forms, gained usefulness on the battlefield.
The rapier is capable of both slashing and thrusting attacks, but the style of fighting popular during its advent and heyday favoured the thrusting attacks we popularly associate with "fencing".
For a more detailed explanation of the primary use of the rapier-- Dueling-- see European dueling sword.
For a thorough and somewhat technical discusion of the rapier see:
- Characteristics of the Rapier by Chris Evans - http://swordforum.com/articles/antiques/char-rapier.php