This is fantastic question and I am sure that there will be many different answers in view of the many epics in the ancient world. In other words, it is not possible to reduce the many epics to espouse one overarching purpose. In view of this, I will offer one reason that seems is loom large over the discussion.
Many scholars believe that epic poetry was written to explain certain things of the past. For example, the Aeneid of Virgil explained the origins of the Romans. Aeneas, a Trojan prince, fled from the burning of Troy to establish a new city at the bidding of the promise of Jupiter. Hence, this epic poem is a foundation story. Something similar can be said for Hesiod's poem (Works and Day). It offers a snapshot view of the gods and the successive ages of time.
The two great Greek works, Iliad and Odyssey also roughly fit into this pattern. By reading these works, the Greeks are able to see from where they have come and possess a self-definition of who they are.
I certainly agree with readerofbooks that epic stories served an important purpose in explaining the past. And I agree that there were other important purposes, too. One was to develop and nurture a sense of pride in one's people, an ancient precursor to the modern notion of nationalism. A story can emphasize the admirable characteristics of one's people more effectively than other means might. Another purpose may have been to encapsulate "history" in a form that could be more easily shared with others. Few people in ancient times were literate, but a good story, or even better, a poem, could be disseminated to others effectively orally. How many of us remember historical dates, as opposed to how many of us remember a good story from history? Even though in many societies literacy is common today, a story or poem always "sticks" so much better. Finally, ancient writers and storytellers had the same impulse we do today, to simply create stories. It is often only in retrospect that anyone infers a purpose to the story. Those in which we see purpose are the ones that survive and become part of the canon.