The 42 generations come from the genealogy of Christ as recorded in Matthew chapter one. It goes from Abraham to Jesus Christ and has 42 generations by the record of scripture itself.
Matthew 1:17 - "So all the generations from Abraham to David are fourteen generations; and from David until the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations."
However, notice that the verse does not specifically state that there are 42 generations. It says rather that there are three groups of 14 generations. This is important because when someone counts through the generations, only 41 can be found in a straight count. But notice the wording of Matthew 1:17. David is included in both the first list of 14 names and the second list of 14 names. He is the fourteenth generation from Abraham, but he is the beginning of another set of 14. That is why the straight count only gives 41 names.
This genealogy is distinct in other ways as well. It mentions five women: Tamar, Rahab, Ruth, Bathsheba ("her that had been the wife of Urias"), and Mary. Not only is this unusual in a Jewish genealogy, but all of them had a tarnished reputation in some way or another. Tamar had her children by acting as a prostitute. Rahab was a prostitute before she joined the Israelites. Ruth was of Moab, a hated people in scripture. Bathsheba had an adulterous affair with David. Mary, though pure, was with child out of wedlock. Rahab and Ruth were also Gentiles; something a Jew would not expect in the line of the Messiah. Certainly, the grace of God is displayed in this record.
Several generations of kings are omitted in the genealogy of Matthew. The kings Ahaziah, Joash, and Amaziah are omitted from between Jehoram and Uzziah. Jehoiakim is left out between Josiah and Jehoiachin. This is not unusual in Jewish genealogies because a grandson is considered a son. Jesus Himself is called the son of David even though there are more than 30 generations between them. We do not know why these particular kings were omitted.
We must understand, that though this genealogy is a true record, it is written to be instructive. Therefore, the facts are presented in such a way as to teach what God desires to teach. This is true in any presentation of truth. Why, then, the 42 generations? I believe it has to do with numerology. Quite often in scripture, numbers are teaching us something and their study can bring much profit. 42 is six times seven. Six is the number of man and seven is the number of completion or perfection. Matthew begins his genealogy with Abraham who received the promise and a covenant from God, counts 42 generations (7 x 6), and comes to the perfect (7) man (6) Jesus Christ.