This is how to write dialogue: (one suggestion anyway): Visualise the scene as vividly as you can. Put the characters in the scene, and give them some conflict or a problem. Then let them at it.
The more you can switch off your internal editor (that little voice which sits on your shoulder telling you you’re doing it wrong), the better. Then it’s just a case of transcribing what the characters say. Don’t be surprised if some things they say surprise you - once you give your creative Self its head, it can come up with some wonderful things.
I find that sometimes the scene flows in so quickly that I don’t have time to punctuate the dialogue properly - that’s fine, just get it down. This is pure creative right-brain work; punctuating is left-brain work, and is a job for another time.
Another suggestion is to act out the scene. Get really into it, and play the parts of the characters - this can be great fun and very productive. (I suggest you make sure not to be in ear-shot of people who wouldn’t understand, however!)
A few things to watch out for:
- Don’t have your characters speechifying. With rare exceptions (which you think hard about and deliberately choose) each character should only have at most two or three sentences before the other character says something.
- Avoid Talking Heads Syndrome, which is what we call lots and lots of dialogue with no description of what the characters are physically doing while they're talking. Make sure that your characters physically move and be expressive while they’re talking - you’ll find out more about that on the page on Dialogue Tags.
- Make sure the conversation flows logically, in one train of thought from start to finish. In real life we don’t do this - rather, we jump all over the place! - but it’s important in fiction dialogue. Each line should arise naturally out of the previous one, in an elegant flow.
- Be very careful not to info-dump. If you’re using dialogue to tell back-story, or exposition, do it smoothly and subtly.
- By all means have the occasional Er or Um for authenticity, but only a very few. Such hesitations and scattiness can also be used for characterisation - to show somebody who’s nervous, for example.
You might like to check out Writer's Digest's video on dialogue too: How To Write Dialogue Like A Pro.