Summarize this article:

Move the bookcase out of the way. Use a yardstick to mark the center of the stud with a vertical line. If this is impossible, you can attach your brackets at the top, into a center stud. For tall shelves, this is the best place to secure a bookcase into a stud because it is the least noticeable place. You can also use door chain latches in place of L brackets if you want to move the shelf regularly. Install the chain on the wall and the slide on the top of the shelf.  Drill the other side of the L bracket into the wall with washers and three-inch wood screws. Drill until the screw head is almost even with the bracket, but avoid stripping the screw. If you can’t find a stud, you should install wall anchors before you drive screws into drywall or masonry. Drill a pilot hole into the wall and push the wall anchor in. Then, align the brackets and drill with three-inch screws. Place an L bracket between the wall and the side of your shelf, where it will hit a stud. Repeat the same procedure on either side of the shelf.
Remove the books. Use a stud finder to locate studs in your wall. Replace your bookcase, positioning it at a point right between the wall studs. Use a stepladder to gain access to the top of the shelf. Place an “L” bracket so that it is flush with the wall and the shelf. Drill the L bracket into the top of the shelf with your cordless screwdriver using screws that will go all the way through the top of the shelf. Ask a friend to hold the bookcase flush with the wall if it is tipping forward. Repeat on either side of the shelf.