That is a wonderful question and it has been the matter of controversy for almost 2,000 years. There are many who assert that when the Apostle Paul speaks of "works of the Law" he is simply referring to the ceremonial laws of the Jewish people. However, this interpretation is unlikely since Paul speaks of the Gentiles obeying the Law by their inward conscience in Romans 2:14. How can the Gentiles be said to obey the Law if it only referred to ceremonial laws? Moreover, in chapter 3 of the same epistle the Apostle states that the Law shuts us up under the condemnation of God and convicts us of our sin and insufficiency to save ourselves (Romans 3:19-20). Again, how can the Law be said to condemn us and show us our sin if it only included the laws of circumcision and dietary restrictions? Another place to go to is Romans 7 where Paul explains that the believer has been divorced from the Law and married to Christ. He then goes on to say that when he attempted to obey the Law he was unable to do so. The key part is where he mentions the specific commandment he couldn't keep which was the 10th commandment, "Yet if it had not been for the Law, I would not have known sin. For I would not have known what it is to covet if the law had not said, "You shall not covet. But sin, seizing an opportunity through the commandment, produced all kinds of covetousness. For apart from the Law, sin lies dead." (Romams 7:7b-8).
Another point that I would like to bring up is that when Paul speaks in other places of justification being by faith he also mentions it being excluded from works in a general sense. For example, in Titus 3:5 the Apostle states, "...he saved us, not because of works done by us in righteousness, but according to his own mercy, by the washing of regeneration and renewal of the Holy Spirit." Notice how the Apostle says "works done by us in righteousness" which without a doubt clearly shows that he does not only have the ceremonial laws in mind.
Augustine also has the same view in relation to this. He says, "'Although, therefore, the apostle seems to reprove and correct those who were being persuaded to be circumcised, in such terms as to designate by the word “law” circumcision itself and other similar legal observances, which are now rejected as shadows of a future substance by Christians who yet hold what those shadows figuratively promised; he at the same time, nevertheless, would have it to be clearly understood that the law, by which he says no man is justified, lies not merely in those sacramental [ceremonial] institutions which contained promissory figures, but also in those works by which whosoever has done them lives holily, and amongst which occurs this prohibition: “Thou shalt not covet.”...Is it possible to contend that it is not the law which was written on those two tables that the apostle describes as “the letter that killeth,” but the law of circumcision and the other sacred rites which are now abolished? But then how can we think so, when in the law occurs this precept, “Thou shalt not covet,” by which very commandment, notwithstanding it being holy, just, and good, “sin,” says the apostle, “deceived me, and by it slew me?” What else can this be than “the letter” that “killeth.”'
Thomas Aquinas also notices this observation in his commentary on Romans 3 where he also says, '"However, a work of the Law is of two kinds: one is peculiar to the Mosaic Law, as the observance of ceremonial precepts; the other is a work of the Law of nature, because it pertains to the natural law, as "Thou shalt not kill, thou shalt not steal," etc. Now some take the Apostle’s words as referring to the first works, namely, that the ceremonials did not confer the grace through which men are made just. But this does not seem to be the Apostle’s intent, for he immediately adds: "since through the law comes knowledge of sin." But it is clear that sins are made known through prohibitions contained in the moral precepts. Consequently, the Apostle intends to say that by no works of the Law, even those commanded by the moral precepts, is man justified in the sense that justice would be caused in him by works, because, as he states below (11:6): "But if it is by grace it is no longer on the basis of works."'
Therefore, it is safe to assume that "works of Law" not only refer to the ceremonial laws prescribed but also the moral precepts and commandments of God. As to what works of the Spirit are, I would claim that any form of obedience, love, or good work done by the believer from a principle of grace would be considered works by the Spirit. A text to look at is Galatians 5:16-18 which states, "But I say, walk by the Spirit, and you will not gratify the desires of the flesh. For the desires of the flesh are against the Spirit, and the desires of the Spirit are against the flesh, for these are opposed to each other, to keep you from doing the things you want to do. But if you are led by the Spirit, you are not under the Law." From this we can conclude that works done through the assistance of the Spirit and from a gracious principle are the works of the Spirit.
Why then the distinction? I believe because if someone is under the moral precepts of the Law they will be under its condemnation. Now those who are under the Law are in bondage to sin and are accursed by God. Sin, therefore, will be aroused by the commandments to further increase its wickedness as the Apostle spoke of in Romans 7 in regards to coveting. With this the sinner begins to hate God (Romans 8:7) because they see Him as their Malefactor and Judge who will righteously condemn them. Because of this, all of their obedience, strivings against sins, prayers and mortifications are done from a legal standpoint. They do these works to justify themselves before God only to realize more and more their utter insufficiency of their own righteousness. They are, as the Apostle says, "held captive under the law," (Galatians 3:23), who also "being ignorant of the righteousness of God," they seek to "establish their own." (Romans 10:3). But after they are justified by grace, their works are done not like how a slave would obey their master, but how a son would obey their father. The greatest difference between both works is that one is done out of legal fear and dread, whereas the other is done out of love.