Baptism for the dead was practiced during New Testament times. Krister Stendahl, the former Luthern bishop of Stockholm and professor at Harvard Divinity School wrote:
In his first epistle to the Corinthians Paul wrote: “Otherwise, what shall they do who are being baptized for the dead? If the dead are not raised at all, why are they being baptized for them” (Conzelmann, 1 Corinthians 15:29).
This verse is part of Paul’s argumentation against those who denied a future resurrection (cf. 2 Tim. 2:18,Justin, Dial. 80). He refers to a practice of vicarious baptism, a practice for which we have no other evidence in the Pauline or other New Testament or early Christian writings. Interpreters have puzzled over the fact that Paul seems to accept this practice. At least he does not see fit to condemn it as heretical, but Paul clearly refers to a distinct group within the Church, a group that he accuses of inconsistency between ritual and doctrine….
[T]he text seems to speak plainly enough about a practice within the Church of vicarious baptism for the dead. This is the view of most contemporary critical exegetes….[I]t [is] quite reasonable that Paul’s remark refers to a practice of a vicarious baptism for the dead. 
Members of the Church use their temples to accomplish this biblical practice. Without baptism for the dead, Christian theology faces several difficult questions: what is to become of the vast majority of humanity who never had the chance to hear the gospel or accept Christ? Will God damn all those who have not had this chance? How can God be just in requiring baptism but making it available to so few?
The doctrine and practice of baptism for the dead solves this issues brilliantly–every person will eventually be baptized vicariously, and it will then be left to them to accept or reject that baptism as they see fit.
Temples also have other purposes. One of the most important involves eternal families.
 Krister Stendahl, “Baptism for the Dead: Ancient Sources,” Encyclopedia of Mormonism, edited by Daniel H. Ludlow, (New York, Macmillan Publishing, 1992), 1:97.
To learn more
- “Baptism for the Dead,” fairmormon.org.