People have always known that when two creatures mate, they produce children that look somewhat like a blend of both, but sometimes with occasionally big changes (they called these "sports"). This "blending" heredity was the "folk" heredity for most of human history.
In the late 19th century, scientists/natural philosophers started getting very interested in the development of cells (embryology), and the question of how somehow organisms can start from relatively small beginnings (and remarkably common ones in the case of animals), and yet end up with very highly-specialized and large results. This focus on "development" was until the very late 19th century the emphasis in thinking about what we would today consider "heredity" — they were not yet thinking about it as a "code" or as "traits."
Darwin's work on evolution, among other things, spawned new interest in what we would today consider to be questions of "heredity," on how traits were passed down. Darwin himself had a theory called Pangenesis that imagined there were small characters of heredity in the blood (gemmules) that were passed into the next generation. This was not ever especially popular, but it did spawn further interest in the topic, notably by Galton's cousin, Francis Galton. Galton is something of the originator of what we would today call the nature/nurture debate and the argument that somehow there is a genetic material that is untouched by the environment and combines in some way to produce the next generation of offspring.
Around the same time as Darwin, Gregor Mendel was doing his work on pea plants and articulating what would later be called Mendel's laws of heredity. It is worth noting that Mendel himself did not see these as universal laws; he thought they were specific instances of what was known as hybridity. In any case, they did not attract much scientific interest in Mendel's day.
By the early 20th century there were multiple scientific camps arguing about how heredity (and evolution) worked. The "rediscovery" of Mendel's work in 1905 spurred new interest in the idea that there were "unit characters" somehow encoded in the cell. Work by T.H. Morgan on fruit flies isolated the genetic material to the chromosome and was able to physically localize certain traits to specific locations on the chromosomes. These are what we call "genes" (a term coined in 1909).
Later work identified chromosomes as being made up of the molecule DNA, and what Watson and Crick did was to figure out the structure of DNA. This double-strand structure led to some immediate insights into how DNA operated on a molecular level, and led to the idea of thinking about DNA (and genes) as being part of a molecular "code" that produced proteins. We are still learning more about exactly how this "code" functions — it is incredibly complicated. (Which is only to say, we do not today really fully understand "DNA's role in the cell" — there are still many mysteries in the area of genes and genomes.)
That's a very basic overview of the history of heredity. There is of course much more to it. The key thing to keep in mind is that the modern question of heredity, e.g. that of "heredity as code," was not how people thought about it until about the mid-20th century. One of the key insights from the study of the history of science is that what we consider the important scientific question at the moment was often not the question that was being asked when some of these things were discovered. Darwin, for example, was far more interested in the development of a cell than he was in the idea that cells had "information" in them to be passed on — he was looking at the development of organisms from the perspective of 19th century embryology. In doing so, however, he did hit upon key questions about what we would today think of as the information within a cell, even though he himself was sometimes quite confused on these points. Galton extracted those points from his work and basically ignored the rest. When later scientists "rediscovered" Mendel's work, they pulled out a few aspects of it and re-interpreted them in the context of their own interests (not Mendel's) — hence me (and most historians) putting "rediscovered" in quotation marks.
My point is, even asking about "traits" presumes a certain model of heredity, one that is comparatively modern. It is one of the fun (and frustrating) aspects in looking back on the past to find that their concerns and their questions are sometimes entirely, radically different from ours today.
My favorite short little history of this is Peter Bowler's The Mendelian Revolution. On the basic issue of people in the past thinking in very different terms — arguably in different worldviews — the classic book is Thomas Kuhn, The Structure of Scientific Revolutions.