When walls or ceilings are water damaged, the first thing to address is fixing the source of the damage (a broken hot/cold water pipe, for example). Once that is done, you should consider the level of water damage: If the drywall simply needs to dry out, wait for the area to dry completely before attempting to paint. However, if the damage is greater, the drywall will have to be repaired before painting can occur.
Repair the damaged area if it needs more work than simply drying it out. Locate and mark the centre of the wall studs at each side of the water-damaged area by using a stud finder. Draw a pencil line down the centre of each stud, so that you end up with two parallel lines, one each side of the water damage. Measure 6 inches above and below the water damage and draw two lines perpendicular to the pencilled stud lines: You will end up with a pencilled square or rectangle shape.
Run a tradesman's knife several times along the pencil lines until the drywall is cut through. Remove the water-damaged drywall and cut a new piece of drywall to the same size as the damaged piece. Attach the drywall to the hole using 1 5/8-inch drywall nails (for walls) or 1 5/8-inch drywall screws (for ceilings).
Apply mesh tape over all the seams -- press it down with your fingers so make sure it has stuck to the seam. Pour joint compound into a drywall tray, and apply drywall compound to the mesh with a 6-inch drywall knife. All mesh should be covered with compound. Wait for the compound to dry completely before sanding the compound to create a smooth finish. If any mesh still shows, apply a second coat of compound.
Brush on a good quality primer over the area. If you are not replacing the drywall, you can choose to apply two or three coats of primer to make doubly sure that the water damage stain doesn't show through -- but wait for the primer to dry before applying a second and third coat. Finally, apply a top coat of paint to tie in the water-stained area with the surrounding walls/ceiling.
Mesh tape is sticky on one side and will stick to the drywall seam. You might choose to apply two topcoats of paint instead of one. This will also help ensure that the stain does not show through. Use 1/2-inch-width drywall for walls, and 5/8-inch-thick drywall for ceilings.