It has become a shorthand matter of faith in contemporary Judaism to speak of the first 5 books of the Tanakh [Hebrew Bible] as having come entirely from Moses, al pi Adonai by the word of our God. Yet this question always was, and probably always will be, messier than that.
"Who wrote the Torah?" is a live question of faith in our tradition, and no single answer could possibly be affirmed with any certainty. Surprisingly, we are not the first generation of Jews to ask the question. Even more surprisingly, we are not the first to accept the possibility of historical development of the Torah over time.
In the Babylonian Talmud, tractate Bava Batra 14b-15a, the classical presentation of the question is best portrayed.
Who wrote the Scriptures? — Moses wrote his own book and the portion of Balaam and Job. Joshua wrote the book which bears his name and [the last] eight verses of the Pentateuch. Samuel wrote the book which bears his name and the Book of Judges and Ruth. David wrote the Book of Psalms, including in it the work of the elders, namely, Adam, Melchizedek, Abraham, Moses, Heman, Yeduthun, Asaph,... etc...The Master has said: Joshua wrote the book which bears his name and the last eight verses of the Pentateuch. This statement is in agreement with the authority who says that eight verses in the Torah were written by Joshua, as it has been taught: [It is written], So Moses the servant of the Lord died there. Now is it possible that Moses being dead could have written the words, 'Moses died there'? The truth is, however, that up to this point Moses wrote, from this point Joshua wrote. This is the opinion of R. Judah, or, according to others, of R. Nehemiah. Said R. Simeon to him: Can [we imagine the] scroll of the Law being short of one word, and is it not written, Take this book of the Law? No; what we must say is that up to this point the Holy One, blessed be He, dictated and Moses repeated and wrote, and from this point God dictated and Moses wrote with tears, as it says of another occasion, Then Baruch answered them, He pronounced all these words to me with his mouth, and I wrote them with ink in the book. Which of these two authorities is followed in the rule laid down by R. Joshua b. Abba which he said in the name of R. Giddal who said it in the name of Rab: The last eight verses of the Torah must be read [in the Synagogue service] by one person alone? — It follows R. Judah and not R. Simeon. I may even say, however, that it follows R. Simeon, [who would say that] since they differ [from the rest of the Torah] in one way, they differ in another.
The essential point here is that a simple reading of the peshat (plain sense) of the last verses of Deuteronomy demands that we conclude that Moses did not author the final verses of the fifth book of the Torah. The rabbis of the Talmud let the words of the Torah and simple logic prove it to them. They could have concluded "Well, God must have given it to Moses as a vision of prophecy," but they did not. They drew the conclusion that the next leader, Joshua, wrote the verses about Moses' death.
The importance of this can not be understated, because in fact there are many places in the Torah where anachronisms or geography force a logical conclusion that a given passage was not written in Moses' time and place. This idea, heretical to some, is not new or modern. As we see, the rabbis of the Talmud engaged in this idea, and so did Rabbi Abraham Ibn Ezra, one of the chief traditional commentaries on the Torah.
Ibn Ezra, who lived and worked in Spain, Italy and Northern France in the 12th century, was the first to genuinely conclude that the examples of later authorship are widespread. In his commentary on Deuteronomy 1:1-2, he points out that the whole narrative of the final book of the Torah is from the point of view of someone who is living already within the Land of Israel. It could not be Moses, therefore, since Moses never entered the Land.
He proceeds to cite several of the examples: Deut 1:1-2, Deut. 21:22, Gen. 12:6, Gen 22:14, Deut. 3:11, and more. For example, Gen. 12:6 reflects, "And the Canaanite was then in the land..." which could not have been written by Moses OR Joshua, since it is reflecting backwards on a past time when there were Canaanites in the Land. In Moses' time, the Canaanite was still in the land, so he could not have addressed the issue in that way. In Deut., 3:11, it reflects that the captured iron bed of King Og of the Bashan is "today in the hands of the Ammonites," who also did not exist at Moses' time. Since it was Moses and the children of Israel who defeated King Og of Bashan, he would still have had possession of it! Only a later author could have written such a line about it being in the hands of some other (later) nation.
Of course, Ibn Ezra's thinking is clear, precise and correct. Whether we like it or not, we must conclude that at a minimum there are a fair number of instances where the lines of the Torah could not have logically been written by Moses. So what? Nu?
There are a few ways you could take this information.
You could, as fundamentalists will, decide that all of these examples are prophetic visions of Moses writing the entire 5 Books of the Torah as we have them today. This is unnecessarily pious and contrary to the ethos of the Talmudic discussion and the teaching of Ibn Ezra.
You could, as humanists will, decide that all of these examples prove that the Torah is entirely a later compilation of human authors whose works are redacted deliberately, but unskillfully, into a large hodgepodge of passages. This is unnecessarily critical of the unity of the final document, and is dismissive of centuries of Jewish belief in the divinity of these books and their ultimate value as sacred beyond mere human ingenuity.
Both of those options are equally strong positions to take, but in my mind are extreme and unhelpful.
Inbetween those bookends you have a range of possibilities - Ibn Ezra and others have claimed that these are all entirely books of prophecy, but that much of the Torah is written by later prophets, not Moses. This idea should be entirely comfortable for the religious Jew. It endorses the sanctity of the Torah and God's role in its authorship, while affirming the peshat of the Torah as being both sensible and authentic.
The alternative middle road, from a religious view, would be to say that most of the Torah is "authentic" from Moses, but certain lines got added, tinkered with, or errantly changed by later scribes. This is also logically possible, but the least palatable, as it makes the Torah a receptacle of widespread error and "forgery."
There is, therefore, only one path which is reasonable, authentic to the words of the Torah, and entirely faithful to the God of Israel as author of both Jewish sacred writ and Jewish sacred history as found in that writ: The Torah is a compilation of prophecy over a long period of time, largely written in the Land of Israel long after Moses had lived and died. "And God spoke to Moses, saying...." is a refrain to be understood in its most simple and direct meaning. A narrator is remembering for us a time when Moses and God spoke, and he is conveying the content of that conversation.
Did Moses write "Sefer HaTorah HaZeh," this very book of the Torah? Yes, he most certainly wrote a Book of Instruction (Heb. Sefer Torah), and the content of that Book is still with us. But the Torah which we have is that PLUS much more.
The fun part is trying to figure out which is which, and when they came from. That is the work of biblical criticism - to identify the contexts and origins of the prophecies in the Torah. It is still sacred work, and it is not necessary to experience it as an attack on Judaism.
Why does it matter?
1) It is true. And truth matters, even when it is inconvenient.
2) It preserves the Torah in the face of secular critique.
3) It broadens the kinds of study we can engage in when we seek meaning in the Torah.
4) It recaptures the talmudic spirit of inquiry in an age of religious reactionaries.
5) By allowing the use of the human intellect, Judaism can remain relevant to the greatest scientific and secular minds of our people, not just those who already accept all of its teachings.
Truth is truth, God is truth, Torah is truth.
Enjoy it all.