Either will work fine. On the grounds that full-on, high quality floor-to-ceiling, wall-to-wall shelves are unlikely to change, I'd probably put them in before the carpet (or cut the carpet out to fit them, if it's already been laid). For some elaborate "Ikea" shelves that will probably come out or change in a few years, I'd put them over the carpet.
Shelving before carpets:
You can do all the messy work of getting your shelving into place without worrying about your carpet. Your carpet won't cause issues with the fitting either (as you'll be fitting with a solid floor beneath the shelves).
If your floors are square/level you may need some wedges under the shelves to get them level before you secure them to the walls. These will all be hidden nicely by the carpet when that gets laid in front of the shelves.
You'll need to think about how to "terminate" the carpet against the bottom of the shelves. You may use one of those metal grippers, but that's extra expense and probably won't look that great. You may choose to put a sort of "skirt" around the bottom of the shelves so that it overhangs the carpet a little bit, and so hides the rough edge. You can also sort of fold the carpet over at the edge to make it straight - although it typically needs some nails through it to hold it down. Obviously, if you take the shelves out you'll need a new carpet (or something to fill the gaps).
Carpet before shelves:
If the floors are slightly uneven or not level/square then the carpet will compress more or less under the shelves to hide this problem. Since the shelves will be secured to the walls, it won't matter structurally, but it'll look nice at the bottom. Since the carpet disappears under the bottom of the shelves, you will have a "clean" edge between carpet and shelves too.
You'll put a bit of extra carpet in place that you don't really need. You may have issues with the carpet not being solid, and if your floors really aren't level you may need some wedges or something under the shelves (which won't look nice). You obviously can't change the carpet without removing the shelves first.
If you take the shelves out, there will be compression marks on the carpet - it depends on the type of carpet if these will really go away or not, plus the carpet will probably be a different colour to the rest (which may or may not be alleviated by cleaning).