In historical terms, a legacy is something that is handed down from one period of time to another period of time. Often it means something handed down from an ancestor or predecessor. Metaphorically, leaving a legacy is planting seeds in a garden you never get to see.
In loose terms, it is sometimes said that a countries or civilizations can leave a legacy, such as an idea that will be remembered for a long time. That is clearly an extension of its original meaning(s) which were about individual people.
More than 2000 years ago the Greek Mathematician, Euclid of Alexandria, collected and wrote down ideas about geometry and measurement in a text called Elements. Students still use these ideas when they learn about mathematics. Other sciences also owe inherited much from Ancient Greece, and science, in general, is a system of legacies. Greek science developed from Ancient Egyptian and Babylonian science, as Islamic science used the legacy of Greece. Renaissance science built on its predecessors, as did modern science.
In Athens, citizens voted on what the city would do. This early form of democracy is a cultural legacy. Ancient Rome elected some of their leaders and made Roman laws that became a legal legacy for later civilizations. Roman and Greek architecture are also often imitated, as another kind of legacy.
Some families pass objects and ideas down from generation to generation. These heirlooms and ideas can also be called legacies. It may have to do with a person or many people. Individuals can leave a historical legacy.
Legacy is a similar concept as inheritance and heritage. It is something we inherit from past generations and pass to our future generations. Usually, heritage refers to material and economical inheritance, while legacy refers to immaterial and cultural inheritance.
When software source code is reused in new software development, the old code is often called legacy code.