I'm asking this question as I've noticed that scientists like Gauss, Newton, Euler, Lagrange etc developed theories in many scientific fields(these ones that I know of were mostly interested in math and physics). When I asked my physics teacher, he told me scientists were "the jack of all trades" back then, they were doctors, mathematicians, physicists, chemists etc. But then I remember that most Ancient Greeks mostly spent their time researching math. So how did the lineage evolve? Also when did science categorize itself into fields and then subfields?
Some of the statements in your question are questionable:-) Among the "ancient Greeks": Archimedes was a mathematician, physicist and engineer. (Equally famous in all these areas). Hypparchus was an astronomer and matematician, doing not only mathematics related to astronomy but also number theory, for example.
The legendary Pythagoras, according to the legend, was the founder not only of mathematics but also of physics and music theory. It is he who discovered (according to the legend) the relation between the length of the string and the musical tone which it produces: probably the earliest true experimental discovery in physics. He was also the founder of a quasi-religious philosophical sect of Pythagoreans.
Ptolemy was an astronomer, mathematician, geographer, physicist and astrologer. His output covers almost all sciences that existed at that time. Hero of Alexandria was a mathematician, physicist and engineer. And this list can be continued.
In the past, when the total amount of scientific knowledge was relatively small, scientists were not differentiated as in later times. Mathematics and astronomy differentiated at an earlier period (in the ancient times), but until the later part of the 19-th century many mathematicians were also astronomers. (The last great mathematician who worked as an astronomer for his living was Pierre Fatou who died in 1930.
As scientific knowledge grows, various disciplines gradually separate from each other. More it grows, more they separate. Separation of mathematics from physics was a slow process which occupied 18-th, 19-th centuries, and continued well into 20-th century.
This process of specialization and separation continues. It is frequent nowadays that people in one area of mathematics do not talk to people in another area of mathematics. In my life time "computer science" separated from mathematics, and "logic and set theory" become essentially a separate science.
So one cannot say that "science categorized itself into fields and subfieleds" at some definite time. This is a continuous process which started almost at the birth of science and continues today.
It is another thing that many mathematicians until 18-th century practiced medicine, law or astrology, or had clerical positions. One had to make a living somehow. There were VERY few positions for mathematicians. As late as in the second half of 19-th century, two of the most famous British mathematicians, Cayley and Silvester, had to practice law for substantial periods of their careers.
This post, The history of “scientist”, traces the history of the term scientist, which was not introduced until 1833 (although science is much older).
As for the current taxonomy of science (the division into subfields), that would require a very long discussion. For example, Michael Faraday's discoveries united what we would now call chemistry and physics. I don't believe Faraday himself saw a sharp distinction. Roughly speaking, as the sciences became more professionalized, the modern taxonomy evolved during the late 19th and early 20th century.
Lavoisier listed Lumière (light) and Calorique (heat) as the first elements in his list of 23 elements. Nowadays we regard the study of heat and light as part of physics, not chemistry, although of course heat plays a major role in chemistry.
In a beautiful and celebrated passage from his Lectures on Physics, Feynman emphasized the artificial nature of the division:
A poet once said, “The whole universe is in a glass of wine.” We will probably never know in what sense he meant that, for poets do not write to be understood. But it is true that if we look at a glass of wine closely enough we see the entire universe. There are the things of physics: the twisting liquid which evaporates depending on the wind and weather, the reflections in the glass, and our imagination adds the atoms. The glass is a distillation of the earth’s rocks, and in its composition we see the secrets of the universe’s age, and the evolution of stars. What strange array of chemicals are in the wine? How did they come to be? There are the ferments, the enzymes, the substrates, and the products. There in wine is found the great generalization: all life is fermentation. Nobody can discover the chemistry of wine without discovering, as did Louis Pasteur, the cause of much disease. How vivid is the claret, pressing its existence into the consciousness that watches it! If our small minds, for some convenience, divide this glass of wine, this universe, into parts—physics, biology, geology, astronomy, psychology, and so on—remember that nature does not know it! So let us put it all back together, not forgetting ultimately what it is for. Let it give us one more final pleasure: drink it and forget it all!
Aristotle (384–322 B.C.E.) refers to the branches of learning as “sciences” (epistêmai), he groups both empirical and nonempirical pursuits into what he calls natural sciences such as physics. He also makes an distinction between theoretical science from more practically oriented studies. Theoretical sciences is what Aristotle calls first philosophy, or Natural philosophy which includes studies such as biology, botany etc. Prior to Aristotle this distinction was not applied, and all studies was encompassed by the term philosophy (love of wisdom). Also it might be good to look at the etymology of the word 'science', and it appears to be from late 14th century but with a slightly different connotation. Namely "knowledge", "collective knowledge", etc. The modern scientist, i.e. an individual who uses the scientific method. We may want to look into Karl Popper who is known for his rejection of the classical inductivist views on the scientific method, in favour of empirical falsification. Which is in use today.