Its purpose is not known – if there even is a purpose.
There has been some speculation that tiny amounts of endogenous hallucinogens may play a necessary role in that periodic alteration of consciousness we call “sleep,” – more specifically dream imagery. This has not been confirmed.
An important thing to remember is that not everything is purposeful – despite the profound impression to the contrary that you may experience if you ever have more than a threshold dose of hallucinogenic tryptamines.
There is an account of theories on the role of N,N-DMT in this paper: Jacob and Presti, Medical Hypotheses, 2005, 64, 930-937. If you don’t have access to a library, maybe try googling for Trace-Amine receptor. The authors say that N,N-DMT has activity at this receptor and hypothesise that it may be a neurotransmitter with an endogenous anxiolytic role.
I’d brush up on Googling skills as a search yielded >3K hits. Threads about psychoactives and hallucinogens are better saved for other places, as the Reader and SDMB admin frown upon such discussions, from what I’ve observed. YMMV.