How to write a Cover Letter for applying to a PhD programme
There's plenty of examples on the Web of the basic format a cover letter should take. There's a good template here on page 23 from Harvard. (Make sure to do it in TeX if applying for a math or comp. sci. PhD.)

Otherwise, my own personal advice ...

What not to do

The most common mistake I have found in cover letters is that they are too generic. Either they could have been written by anyone ("I have a keen interest in science. I was always curious as a ...") or could have been written for any programme ("I want to do a PhD as I believe I would be well-suited to a career in academia ..."). Keep such generic sentences to a minimum: the more of these a cover letter has, the more impersonal and unremarkable it is, and the more it leaves the suspicion that the same cover letter has been recycled for multiple applications and/or by multiple students.

Also, avoid hyperbole ("I am the best candidate for this position because ..."). You cannot know that you are the best candidate. This is an academic application, not an industrial one. 

What to do

Make your cover letter personal, remarkable (i.e., stands out from other cover letters), specific to you and specific to the position at hand. Be enthusiastic. Be specific. Show that you've put thought into the position and why you are applying.

Relate your specific skill sets and previous experience to the programme you are applying for. Relate the content of specific aspects of your CV to the programme: 

"During my masters, I enjoyed working on the topic of A, which relates to your programme [in this way]"

"I worked three summers at company B, where I gained experience in topic C ..." 

"I visited your university in March last year and was impressed by ..." 

"I read paper entitled 'D' published by your group at 'E' and was interested in ..."

"I recently published a paper 'F', which I believe compliments work by 'G' in your department on 'H' ..." 

... and so forth.