Sometimes poskim will pasken from the Talmud (gemara) instead of the Shulchan Arukh. However, I thought that we pasken from the Shulchan Arukh and not from the Talmud. What is the proper way for deciding the halacha?
The Talmud is the source of Halacha and not the Shulchan Aruch. The Shulchan Aruch, on the other hand, is really 'only' a useful guide as to which opinions within the Talmud are accepted as normative Halacha, and what normative Halacha considers to be proper applications of the numerous general concepts found in the Talmud. In that sense the Shulchan Aruch (with the Rema) is the most major/accepted Halachic authority, but the Talmud is still the source of all Halacha, and in theory the only authority a Posek needs to answer to.
Although I cannot remember where I learned this, I recently learned that the Vilna Gaon (a.k.a., the G.R.A.), one of the most knowledgable rabbinic luminaries of all time, famously innovatively taught his students that when there was a disagreement between the views of the Talmud and the Shulchan Arukh, it was better to follow the view of the Talmud.