Why is Jesus' genealogy in Matthew and Luke traced back to David through Joseph if Jesus was conceived by the Holy Spirit?
First of all, Joseph was not Jesus biological father in any understanding since Mary conceived by a miraculous intervention of the Holy Spirit before she was joined to Joseph and the text tells us they refrained from intercourse until after Jesus birth.
However in the eyes of the law of the time, Joseph was the father. More than just a legal guardian, by later taking Mary as his wife, he also claimed Jesus as his son. This gave him a standing in relation to the child that -in the eyes of the Jews at least- must be accounted for. The fact that he was himself of the line of David would put many doubts about the validity of Jesus' claims to rest. The excuse that there was no blood relation would have been overshadowed by the fact that he was the lawful father and not himself of the prophesied linage.
Knowing that Joseph was himself of the line of David makes Jesus the rightful legal heir to the throne of David even though he wasn't biologically related to his earthly father.
Secondly, there is actually a huge difference between the genealogies found in Matthew and Luke. If you read them, there are only a couple names in common from David to Jesus. One possibility for this is that Joseph had two fathers: one legal and one biological through a Levirate marriage (where the brother of a man with no offspring would step in to provide one).
However the other idea that many scholars agree on is that the record found in Luke is actually a trace of Mary's family tree. It wasn't common to trace the maternal side, but then again the virgin birth was hardly a normal occurrence! This would mean we have both the paternal and maternal genealogies and all the bases are covered.
Short answer: because both the right to be king (which Matthew traces) and the legal guardianship (which Luke traces) came through the father.
Long answer: Matthew's genealogy is traced through the right to be king. A theme in Matthew is the kingship of Jesus. This starts in ch1, where Jesus is called the "son of David" (Mat 1:1) - Joseph is called the "son of David" only a few verses later (Mat 1:20). Matthew's emphasis here is that Jesus, the legal (not biological) son of Joseph, was counted as a son of David because Joseph was a son of David. This phrase harks all the way back to 2Sam 7:12-14, which say that David's "seed" will be set up as a king and established forever, and be God's son. Though this prophecy may be partially fulfilled by a few Old Testament kings, it is completely fulfilled in the Messiah. This prophecy can be traced through the Old Testament in places like Is 11:1 (talking about the "stem of Jesse" [Jesse was David's father] producing the Messiah who would reign), cf. Is 16:5, Jer 33:15-17. This phrase "the son of David" is therefore used as a synonym with "Messiah", emphasizing Jesus' right to rule - the phrase is used more times in Matthew than in the rest of the New Testament combined (Mat 1:1, 20, 12:23, 15:22, 20:30, 31, 21:9, 15, 42). Note also Matthew's emphasis on David's kingship in his genealogy in 1:6 - an emphasis found in none of the other kings in Matthew's genealogy.
This theme of Jesus' kingship, which starts with the term "son of David", can be traced through Matthew as the wise men worship him as king of the Jews, as Jerusalem is told, "behold, thy King is coming, lowly and sitting upon a donkey", as Jesus at his trial admits to being the king of the Jews (Mat 27:29), and even as this same epithet is written over the cross as he dies. The kingship theme in Matthew is undeniable.
Because of this emphasis on "the son of David" and kingship in Matthew, the trace of any genealogy but through the line of kings would be surprising. Moreover, one of the people in the genealogy, Jehoiachin (Matthew calls him "Jeconiah"; he is also called "Coniah"), had no children: Jer 22:30, "write this man down as childless". Thus the genealogy cannot have been biological. But as the right to be king passed from this childless man to the next man in line, a a genealogy tracing kingship rather than genes has no problems with a man having no children - the genealogy simply skips to the next man in line for kingship.
Luke's genealogy is traced through legal parenthood.
Sorry, gotta go. If I have time, I'll finish the answer later - or make it a wiki, and someone will finish it for me.