Lay the headboard flat on the floor with the face of the headboard positioned upward.
Place a bed rail against the leg of the headboard on one side. Adjust the rail up or down, depending on how high or low you want the rail to fit against the headboard. Once you get the desired position, mark the headboard where you want the bed rail to fit. Make two marks for each side for extra support. The marks should be about 3 inches apart. Repeat this process for the other side of the headboard.
Drill pilot holes into the back of the headboard. Align these holes with the marks that you made on the front of the headboard. These holes should be 3/4 as large as the lag bolts. For example, if you are using 1/2 inch diameter lag bolts (a common size for headboards with 2-by-4-inch boards), use a 3/8 inch drill bit for the pilot holes.
Position a washer over each lag bolt.
Prop up the headboard and align one bed rail against the marks that you drew. Have an assistant hold this rail in place as you insert each lag bolt into the appropriate pilot hole. Tighten the bolts with a ratchet and socket, but do not completely tighten them yet.
Attach the other bed rail in the same manner. Once you get both rails attached, adjust them slightly to the left or right if necessary. When you have aligned the rails properly, fully tighten the lag bolts with the ratchet.