There are several reasons why Shakespeare is portraying the sun as inconstant in Sonnet 18. The first reasons are literal, as we see in the lines:
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd ...
Sixteenth-century English society was primarily agricultural. Farmers depend on regular amounts of rain and sun to grow crops but the weather is indeed fickle and farmers look anxiously at the sun each day hoping to get just the right weather for a good harvest. Sometimes, though, especially in England, there can be many weeks of cool, rainy weather, while at other times the weather may be unseasonably hot.
Also, by its very nature the sun is inconstant, rising in the morning and setting in the evening, and producing long days in summer and short days in winter.
More important for Shakespeare's purpose though is that the stars, including the sun, belonging to the celestial realm, are considered more constant than things in the sublunary sphere and so by claiming his poem to be more durable than the sun, Shakespeare is boasting of the quasi-divine nature of the poet who can create create artifacts that in having a nature that is not physical are more constant than the physical world, a theme we also see in his Sonnet 55.