The fact that Gollum followed Frodo and Sam all the way through Mordor, despite his previous experience of torture in Barad-dûr, emphasizes his utter desperation to reclaim the Ring. He’s no longer tempered by the innocence of Smeagol, the being he once was, and is now completely wracked by the Ring’s temptation. Sam’s firsthand experience of this temptation and his unwavering peaceful qualities lead to him showing Gollum mercy, even though he knows that killing him would’ve been both logical and safe.