Punishment is a negative consequence imposed on alleged wrongdoers. It is probably the oldest concept in law, and quite possibly one of the oldest in human interactions. The concept of punishment is so well accepted that we rarely consider the rationales behind it.
 Aspects of punishment
Retributive justice is based on principles of revenge: making the transgressor suffer for the suffering or damage they have caused. This is almost certainly the oldest form of justice, and most ancient legal systems were based on principles of retribution. A good example is the Torah, which is explicitly vengeful in many places, even stipulating that the relatives of murder victims have the right to kill the perpetrator without blame. The retributive principle is most famously summed up in the tit-for-tat adage "an eye for an eye" (Exodus 21:24, repeated various other places in the Bible).
Capital punishment originated as retributive justice, with authorities taking lethal revenge on criminals on behalf of their victims or society as a whole. Corporal punishment (non-lethal physical punishment) is also retributive, and has been used throughout history by governments, armies, navies, employers, schools and parents. Retributive punishments are sometimes also symbolic. For example, Sharia law, at least in its strictest interpretations, stipulates that thieves should be punished with the amputation of a hand, the appendage which committed the crime.
The problem with retributive justice is that, as everybody knows, two wrongs don't make a right. Taking "an eye for an eye" just results in two mutilated people instead of one. Its only purpose is cathartic, and that is retributive justice's other major flaw: it appeals to the worst side of human nature, our sadistic capacity for taking satisfaction in another's suffering. Hence it becomes questionable whether the punishers are really morally superior to the perpetrator.
Opposition to retributive justice has often been expressed, and reform of retributive laws has been growing in Western civilisation since the Enlightenment, also spreading to other cultures. Death penalties are still used in many countries, but public executions are now a thing of the past in all but the most extreme regimes. However, the retributive principle is still very popular, as expressed in public anger and demands for punishment whenever severe crimes are reported in the press. Prison sentences can also be regarded as at least partly retributive, on the understanding that incarceration is what wrongdoers deserve.
A common facet of punishment is to make an example of the wrongdoer by giving them a punishment which will deter them from committing the same offence, and deter others from following the same path. Arguably, deterrence is the most fundamental principle behind law itself, and even morality: knowing that our negative actions will have negative consequences for us is usually enough to prevent us from taking those actions.
Deterrence is one of the arguments most commonly made in favour of capital punishment, regarded as the ultimate deterrent. Prison sentences are also regarded as having some deterrent element, although this is often weakened for repeat offenders, since criminals usually acclimatise to life behind bars.
Deterrence is also problematic in that criminals don't expect to get caught. If they did, they would not commit crimes in the first place. The decision to commit criminal acts, or other transgressions which could result in punishment, is always a gamble, and the severity of the punishment may be enough to deter some, but there will always be those who believe that they will escape punishment.
Some forms of punishment aim to prevent the offender from repeating the same kind of offence, and hence protect society from them. In many premodern societies, this included permanent mutilations such as castrating rapists, and blinding or crippling thieves or other criminals. Incapacitation is also another common justification for the death penalty, since dead criminals can no longer commit crimes.
Prisons also involve incapacitation, in that, for the duration of the sentence, they remove the criminal from wider society and hence bar them from most opportunities for crime. Historically, exile or transportation were also used as punishments for the same purposes, among other reasons.
Rehabilitation of offenders is probably the issue of greatest concern in modern sentencing, although the concept is a much older one, with early prisons often being known as "houses of correction" to emphasise their intent of "correcting" wrongdoers. Modern prisons endeavour to help convicts rectify their criminal tendencies, teach them new career and life skills, and prepare them for a law-abiding lifestyle after release. Parole hearings assess whether prisoners are adequately rehabilitated to be released back into the wider society.
Critics point out that prisons sometimes have the opposite effect: surrounding convicts with others criminals, in an atmosphere which is often brutal and factitional, may transform petty offenders into hardened criminals. However, this is a criticism of the conditions in prison rather than the principle of rehabilitation.
Capital punishment is the antithesis of rehabilitation, and is used either for those criminals who are judged to be beyond reasonable expectation of rehabilitation, or for those crimes which a society regards as unforgiveable.
Rehabilatative principles are common to many lesser forms of punishment, such as workplace discipline proceedings, or the way parents punish their children ("go to your room and think about what you've done!"). Although these are rarely described as "rehabilitation", the principle is similar: to discourage negative tendencies and work towards better behaviour.
Punishments may involve the offender making amends to the victim, financially or otherwise. Legally, this is far more prevalent in civil law than in criminal law, and the compensation or damages won in lawsuits are not generally considered "punishments" in the same sense as sentences given in criminal court hearings. However, convicts serving prison sentences are often said to be repaying a "debt to society", and some other forms of sentencing, such as community service, specifically involve work for the community that the criminal is thought to have harmed.
 Divine punishment
Punishment by God or gods, either in this world or the the afterlife is a major theme in many religions. Such concepts as Hell serve as a powerful deterrent to believers, while non-believers may regard this as the concepts' only purpose.
If Hell genuinely existed, then its only value as a punishment, beyond deterrence, would be retribution. This raises troubling questions, touching on the problem of evil, as to why an omnipotent entity would choose to exact vengeance on those who displeased it.