The above quote by Immanuel Kant makes a distinction between ethics and law.
Laws are externally enforced upon a society and deter potential crime. Ethics, on the other hand, serve to guide individual conscience by enabling people to separate right from wrong.
Thoughts lead to actions. In law, the legality of actions is adjudged. Similar purpose for thoughts is served by ethics.
For example, a person who thinks about killing another person is not guilty in law as he does not deprive the other person of his right to life. However, simply by harbouring thoughts of murder, he is ethically guilty. Similarly, men who think of violating a woman’s dignity are guilty from an ethical point of view, even though they may not be considered legally guilty until they actually perform that act.
On the other hand, Kant’s statement may not always hold true, as ethics reflect the society’s mindset, which changes with time.
For example, robbing the rights of slaves was considered perfectly legal as well as ethical in the mid-19th century in Southern United States. Similarly, prosecution of Jews and the Holocaust was not considered unethical, and was in fact supported by majority of the German society.
Ethics and laws are complementary to each other. Most laws are derived from ethics, and both of them reinforce each other to create a stable, harmonious society. The purpose of both is to create a just, equitable, rule-based environment for humans to thrive in and flourish.