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.