I am confused about the two genealogies we find for Jesus. Matthew and Luke give differing lines of descent. How are we to understand this?

In the words of H. B. Frank, Jr. (in From Riches to Poverty to Glory: A Study of the Life of Jesus Christ), we find the following helpful explanation.

"The purpose for the peculiarities of Matthew's genealogy [is] not so much to prove the natural connection of Jesus with the family of David -- a fact no one doubted -- but the defend the one vital point where attack had been made, namely whether Jesus was of legitimate birth. The slanderous charge was very early in origin... Matthew meets that slander by giving a bird's eye view of history from Abraham to the Messiah in the form of a genealogy of Joseph, who, in light of all the facts concerning the origin of Jesus, marries Mary and gives her the protection of his stainless name and royal lineage. Thus Joseph is brought forward as a witness for Mary.

"Luke's genealogy moves backward from Jesus to Adam. Between Abraham and Adam it is the same as in 1 Chronicles 1:1-7:24-28. From David to Abraham, Luke agrees with O.T. genealogies and with Matthew. Between Jesus and David, Luke's list differs from Matthew's, except in two places -- Shealtiel and Zerubbabel (Matthew 1:12; Luke 3:27). There is no O.T. record to compare wih Luke's, except for Nathan (David's son), Shealtiel, and Zerubbabel. At this point the two genealogies crossed through adoption or otherwise.

"A major difference in the two genealogies is the name of Joseph's father, who is Jacob according to Matthew and Heli according to Luke. Either these two lists contradict one another here, or else Joseph was in some sense son of both Jacob and Heli. An explanation suggested by most of the 'church fathers' as to Joseph's relationship to Jacob (Matthew 1:16) and Heli (Luke 3:23) involves the Levirate law (see Deuteronomy 25:5-6). They suggest that Jacob married Heli's widow and begat Joseph, who was then the legal son of Heli.

"To account for the different genealogies, [the] widely accepted view is that Matthew gives the legal descent of those [heirs] to the throne of David, through Joseph, while Luke gives the physical descent through Mary. A possibility is that in this way Luke has given us the meeting-point of the lineage both of Joseph and Mary, who would be kin. Matthew is concerned with the kinship of Jesus; Luke, with his humanity. Both make plain his virgin birth, and therefore his deity..."