Measure the length of the back frame of your headboard. Typically the frame will be 2 to 4 inches wide all the way around, giving you adequate room to attach the cleat to the inside edge of the frame. The headboard should be flush against the wall.
Take about 4 inches off the length of the frame and cut a piece of 2-by-4-inch lumber to this length. Rip the lumber in half lengthwise, using the saw to make the cut on a 45-degree angle, rather than straight. This creates a pointed edge on each piece of lumber that can mate together to form a locking cleat, called a "French cleat."
Center one side of the French cleat at the back of the headboard frame on the top edge. Align the edge of the cleat and the edge of the frame on the same plane. The long side of the cleat's angled tip should be at the back, pointing down.
Drill countersunk pilot holes through the cleat into the frame of the headboard, then attach with wood screws sized to fit the pilot holes and long enough to penetrate about 3/4 inch of the headboard frame. Drive the screws in deep enough so the heads don't protrude over the end of the cleat.
Locate the wall studs over the bed, using the stud finder. Use a level to mark a straight line across the studs at the height you want the headboard to hang.
Position the second half of the French cleat on the level wall line, centered over the bed. The short side of this half of the cleat should be flush with the wall, with the pointed end up. This will create a lock when the downward-facing pointed end of the cleat half attached to the headboard is hooked over it.
Secure the second half of the cleat to the studs in the wall, using 4-inch drywall screws. Hang the headboard over the wall cleat, ensuring the cleat section on the headboard securely locks into the wall stud cleat.