Shakespeare's intention when writing Sonnet 18 wasn't only to compare his friend's (or rather, his lover's) beauty to a summer's day, but to also point out that, in his eyes, the man's beauty and his gentleness transcend the beauty of summer, which might be unmatched in the eyes of many, but it is also fleeting. He describes his beloved as "more livelier and more temperate" than summer and insinuates that even summer, the loveliest of seasons, is not as beautiful and as magnificent as his lover.
Summer can be too hot and too unpleasant at times, as this is how nature operates, and it's not too long before it passes and thus loses its beauty.
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date;
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature’s changing course untrimm'd;
However, there are also days when everything is simply perfect—and it is these days that best describe his lover. Thus, Shakespeare compares his lover's beauty to one of these all too wonderful and all too lovely days and calls it "eternal summer"—one that is endlessly pleasing and wonderful, and one that will last forever.
The reason he wrote the sonnet is essentially to commemorate and immortalize his lover's beauty and existence in general; neither his beloved lover nor his beauty will die or be forgotten, because he will live eternally in Shakespeare's lines. As long as people continue to read this poem, as long as this poem "lives," so will his lover.
But thy eternal summer shall not fade,
Nor lose possession of that fair thou ow’st;
Nor shall death brag thou wander’st in his shade,
When in eternal lines to time thou grow’st:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee