The Maharsha (Sotah 22a, chiddushei aggados sv. yerei) writes that one may not rule on an halakhic matter by simply looking in the Shulchan Arukh:
In these generations, those who rule from the Shulchan Arukh -- they do not know the reason for each matter unless they first study the subject from the Talmud, which is called "shimush talmidei chakhamim" -- a mistake falls in their rulings and they are among those who destroy the world. It is proper to rebuke them.In other words, the Maharsha considered the Shulchan Arukh to be a book for cheaters who do not really know the halakhah. If you just look up a law in the Shulchan Arukh, you are missing the background and the overall context. You, therefore, might misunderstand and incorretly apply the Shulchan Arukh's ruling.
The Maharsha was writing in the early 17th century, only decades after the Shulchan Arukh was first published. Other critics of ruling directly from the Shulchan Arukh include the Maharal and the Bach (more on this in a few weeks).
However, the Pischei Teshuvah (Yoreh De'ah 242:8) writes:
It is possible that this was only in the time of the Maharsha, when there were not yet any commentaries on the Shulchan Arukh. However, now that the Taz, Shakh, Magen Avraham and other later commentaries were written, and every ruling is explained sufficiently in its place, it is proper to rule from the Shulchan Arukh and its commentaries.In other words, the commentaries add the necessary background to the Shulchan Arukh's rulings so that you can properly apply those rulings.
Then what do you need a rabbi for, assuming you know what's in the Shulchan Arukh and commentaries? For those cases that are not directly addressed in the Shulchan Arukh and commentaries, when it is necessary to compare cases and evaluate conflicting imperatives.