The key to sturdy floating shelves is to securely anchor to the studs. Dry wall anchors aren’t strong enough since there is no lower bracket for leverage and support.
For solid shelves you have to use concealed floating shelf brackets that are made from strong materials, usually steel or aluminum. These brackets will match or be slightly smaller than the thickness of your shelving material. You will use lag bolts or screws that anchor to the 2×4 wall studs behind the drywall.
The strongest floating shelves that hold alot of weight and use blind shelf supports will have rods that protrude well into the shelf through holes drilled deeply into the back edge of the shelf. The holes will need to be drilled precisely to match up with the bracket rods. You can buy kits that come with the fasteners, brackets and pre-drilled shelves.
Click Here to purchase the brackets.

If you don’t need to hold a lot of weight other designs like a u channel secured to the studs and then screwed into the back part of the shelf are an easier do-it-yourself option.
Box shelves can be assembled with simple L brackets starting with a piece of wood secured to the wall studs and the rest of the box secured with screws to the backing piece mounted on the wall. These are also popular kits and look very sleek and modern, like furniture attached to the wall.
Click Here to buy kits.
For reference lots of photos http://www.pinterest.com/shadowphrost/diy-floating-shelf/