For Jesus to qualify as the Messiah, as you correctly stated, he had to be not only a Jew but also be descended from King David, who came from the tribe of Judah (Luke 1:32). As an interesting side note, the term Jew was originally used only for those who were the descendants of Judah (one of Jacob's sons). We will first briefly discuss some history before delving into the lineage of Jesus.
Hundreds of years before the birth of Jesus the united kingdom of Israel split after the death of Solomon and broke into two major parts. The southern part of the kingdom, composed of three tribes, became the Kingdom of Judah. The term Jew, sometime after the break, began to be used as "shorthand" for anyone who lived in the southern kingdom.
Determining factors
Your inquiry assumes that it is solely or primarily the lineage of a person's father (in this case, his stepfather Joseph) that determines the "race" of any children he sires. The Biblical truth is that the descent of the mother, in this case Mary, is more important in determining whether a child is considered a Jew (in this case, springing from the tribe of Judah) than that of the father. In other words, if Mary were Jewish then Jesus would be one as well.
For example, a baby born with a mother from the tribe of Judah and a father who is a Gentile (non-Israelite) would be considered a Jew. A baby produced, however, through a father who is from the tribe of Judah and a mother who is a Gentile (non-Israelite) would not.
Doubts about the true parentage and lineage of Jesus abounded in the first century, as most people doubted and even rejected the idea that he could have be born from a virgin (even the Pharisees hinted at him being born illegitimately, see John 8:41).
How can we prove the lineage of Jesus qualifies him to be the Messiah if the Hebrews, in general, did not include women in their genealogical tables? The answer is that the gospel writer Luke resorted to a little 'trick' in order to record that the bloodline of Jesus, through Mary, went all the way back to King David, then Judah, and then all the way back to Adam.
Luke begins tracing our Savior's physical line through Mary by stating her husband, Joseph, was the son of Heli (Luke 3:23). Joseph's father, however, according to the book of Matthew (Matthew 1:16), is not named Heli but Jacob. Heli is the name of Mary's father (the father-in-law of Joseph).
Luke uses Joseph's name to represent, symbolically, Mary in the lineage he records. Jesus, therefore, is a Jew, through the tribe of Judah, by blood through his mother.
The book of Matthew, chapter 1, traces the descendants of Abraham to Judah, then to King David, then to Joseph. What does Matthew's lineage show since, as you rightly state, the real Father of Jesus was God in heaven (Matthew 1:18, 20, Luke 1:34 - 35)?
Matthew reveals our Savior's legal lineage, which means he had a legal right to be considered a son of David. The New Testament confirms this when it states, "The book of the genealogy of Jesus Christ, the Son of David, the son of Abraham" (Matthew 1:1).
The pedigree of our Savior Jesus is also asserted in other places in the New Testament as well, such as Romans 1:2 - 3, which states he came from the seed of David. In Hebrews 7:14 it emphatically states, "For it is quite evident that our Lord has descended from Judah . . ." (see also Acts 13:22 - 23).
Jesus is both, by blood and legally, a Jew and a descendant of David. It was Joseph's lineage, back to the king, that forced him and a very pregnant Mary to travel to Bethlehem.