Melatonin is a hormone that helps you sleep well. A consequence of modern life is that the amount of melatonin your body makes, and when it makes it, is often different from how it used to be in our ancient environment. Taking some extra melatonin when you want to go to sleep can ensure there is enough melatonin available in your body for you to sleep well.
The bottom line
Melatonin is safe1,2 and cheap. The optimal dose is 0.3mg shortly before going to sleep2. Of course it is always wise to check what reputable sources have to say3,4.