Getting a child's little fingers and hands to grip the pencil correctly is the first step to a lifelong habit of comfortable writing. Using the correct grip allows quicker and smoother writing as well as preventing some physical problems that can come along later in life, such as carpal tunnel syndrome. But at the same time you don't want to discourage the child when they first begin either.
Have the child curl all of the fingers in toward the hand and then extend the index finger and thumb.
Rest the pencil on the curled middle finger and along the space between the thumb and forefinger.
Tell the child to pinch the pencil with their thumb and forefinger.
Ask the child to then relax their grip so that they are comfortable. The eraser end should point toward the shoulder. If it is straight up, don't worry about it as long as they have the grip right at this point.
Have the child practice their grip for a few minutes everyday. Don't overdo it. Learning these fine motor skills takes a while. Make it fun. Try drawing or coloring to keep it interesting.
Some children will bear down on the pencil to see the lines they are making. Give them soft-lead pencils that mark the paper with less force. Soft lead pencils can be purchased at art or stationary stores.
You can buy formed pencil grips with indents where the thumb and fingers should be placed.