A rail that's pulled loose from one or more of its posts may or may not be salvageable, depending on how badly rotted the joint is. You may be able to mend the break with a 2x4 scrap or a couple of metal T-braces, or you may have to replace the entire rail. To repair a rail with a 2x4:
Step 1: Before you make repair, saturate the damaged areas and a 2x4 liberally with a wood preservative. This keeps the rot from spreading.
Step 2: To make a cleat to support the rail, make sure the rail is level, then fit a 2x4 snugly underneath. Nail the 2x4 to the post with galvanized nails. Drive a couple of nails down through the rail into the cleat.
Step 3: Carefully caulk the top and sides of the repair to keep out moisture.
Galvanized steel T-braces, available at most hardware stores and home improvement centers, are somewhat less conspicuous and often make a more lasting repair. To use T-braces in rail repair:
Step 1: Level the rail, then drill pilot holes into the post and rail.
Step 2: Secure the T-braces to the rail with galvanized screws.
Step 3: Caulk the joint.
Step 4: Paint the T-braces to match the fence.