Mounting bookshelf in Closet
If the shelf is one with a sturdy back that is affixed to the shelves and sides of the unit then you should be just fine using some screws like 2 to 2.5 inch long #8 or #10 screws. Three or four on each side that are through the back, through the wall covering (drywall) and into the studs should be more than enough hold it securely. 

On the other hand if the shelf is the very typical type with a thin cardboard back that is stapled to the back then that will not be suitable to use to mount the shelf against the wall. Another technique would be required. One scheme would be to screw some angle brackets onto the sides of the shelf and then through the other face of the angle brackets into the studs. This of course only works if the angle brackets just happen to line up over the studs. These are the types of angle bracket I am talking about. 





If careful measurements reveal that the angle brackets above do not end up lining up with the studs on both sides of the shelf then it will be necessary to pre-attach several boards to the wall that are longer than the width of the shelf unit and long enough to span across to where studs are located. Screw three of these boards to wall with screws into the studs. Then use the aforementioned angle brackets to the sides of the shelf unit and into these pre-attached boards.