Murder is against the law; killing absent unlawful motive or negligence is not. Hence, the state can lawfully kill someone once their guilt is determined, their due process rights respected, and the penalty determined to be reasonable given the crimes committed. An individual whose life is at risk by the actions of another individual can also kill, legally. Justifiable homicides happen all the time. If I break into your home to try and murder you, you can kill me and the state has no interest in prosecuting your causing my death.
Capital punishment doesn't bring anyone back to life and if we're honest with ourselves, it likely has little deterrence effect on other criminals. However, the benefit is that it stops an individual who is so dangerous and destructive that society cannot afford to risk their continued existence. Locking them in prison subjects other prisoners, guards, staff, and even other members of society to varying levels of risk from that individual. When an individual is found to exhibit a certain level of danger and destructiveness, society's best option may just be to end that individual's existence in a fair and lawful process.