A query letter is a brief letter (less than one page long) that does the following things:
1. It introduces your book in a way that makes it sound enticing to read.
2. It demonstrates your ability to write coherently.
3. It introduces you as an author.
4. It provides agents and editors with a means of contacting you.
That's it. It does NOT:
1. Make predictions about how well a book will sell.
2. Insult anybody, ever.
3. Tell all about your life and your passions.
4. Do anything except for the four things listed above.
There are a few different formats on query letters, and every agent and editor will have a different bit of advice for you, but this is basically how it goes:
Dear Agent's First and Last Name,
This is a brief blurb about my book. It is told in third person, present tense. It doesn't name too many characters or use too much world-building jargon, and it is clear and concise. It sounds a lot like the blurb on the back cover of a novel. In fact, many back-cover-blurbs have been lifted directly from query letters. This blurb should also match the tone and voice of your novel.
The blurb can be one or two paragraphs, but probably not three or more. You will introduce your main character (or both main characters if you're writing romance) and what their main goal and conflict is. You will not reveal the ending, but you will also not leave it with a rhetorical question. You will write in a normal font, of a normal size. You will use standard industry formatting.
This next paragraph will be about the book. The title will be in ALL CAPS. You will list the category, the genre, and the word count (rounded off to the nearest thousand and written like this: 95,000 words). You might mention that it's part of a series, but always emphasize that this title stands alone, regardless. If your agent or editor of choice specifically asks, this would be a good place to list comp titles.
The very last paragraph is about you. Your writing credentials, starting with the most relevant first. If you don't have writing credentials, you can either skip this or talk about your writing platform or social media presence, but do so briefly. There is not one agent who cares more about your twitter followers than they do about your book.
Thank you for your time and consideration (this is your only option here, do not vary from this),
Your First and Last Name
And that's it. That's the basics of it. Write the query letter first, and then go diving into some of the following resources to help you fix it. Why do it in that order? Because you can't fix something that isn't there.
Agent Query - How to Write a Good Query
Query Shark - A real, live agent takes real, live queries and tells you why they suck and how to fix them. Read the archives. This is why you need to have your letter written first. As you dig through the archives, you'll see things that you did. And you'll fix them, one by one. And then you'll do more things wrong. And you'll fix them.
The Girl with the Green Pen - a veteran slushpile reader will critique your query for free. Three times. And then if you pay her only $15, she'll fix it as many times as you need. Even a hundred. She's legit, too. I wrote a post on MMW about Taryn's Query Day: She read hundreds of query letters, critiqued them all, and tweeted the best bits of advice all day long.
Kristin Nelson also wrote a fantastic post about query letters.