Draw a light line with a pencil along the top of the bookcase.
Slide the bookcase away from the wall. Empty it first if it is full, so you don't hurt your back or accidentally topple it.
Line an "L" bracket up with the pencil mark on the wall. The L shape will look upside down. The bottom of the "L" that sticks out needs to be lined up with the pencil mark. If the top of the bracket is lined up, the bookcase will not slide back underneath.
Screw the "L" brackets into the wall using appropriate screws. For example, use drywall screws for drywall. Screw the brackets in on a stud. Use a stud finder to find a stud if you do not know where they are. Put at least one bracket on either side of the bookcase. If you have a wide bookcase, you may want a third bracket in the center.
Slide the bookcase back against the wall and position it as desired. If you measured properly, the bookcase will slide snugly under the brackets.
Screw screws down into the brackets from the top of the bookcase. Make sure you use screws that are short enough that they won't poke through the other side of the bookcase.