The problem lies in the automatic exposure mechanism, and the fact that the setting sun can throw your exposure meter way off. However, there is an easy fix. What you need to do is:
aim just above the setting sun, and press and hold your shutter half way, then recompose the photo as intended and then shoot.
Make sure that while pressing the shutter half way, that the sun is not visible in the frame. The point is to get the exposure value reading from the sky itself without the sun locked in, and then use it to shoot the photo as originally intended.
Alternatively you could use Auto-Exposure Lock feature (AE-L) if your camera has it instead of half pressing the shutter. Additionally, if you want to get creative with your sunset shot you might want to check out our article:
Let us know how it went. Happy shooting.