How would you have handled the moment? Would you have provided another method of him getting out? Would you kill him off anyway? Would you kill someone ELSE off in the process? Etc. My take: Instead of the ship flying down deus-ex-machina-style, Sam decides while looking at him helplessly that "Hey, he's saved my life a thousand times; I'm so stupid for never repaying him" and runs out to divert Soundwave. This causes Bee and the 'Bots to escape, but Sam's leg gets hit by Soundwave's cannon and is blown off. Ratchet sees this, carts him away in vehicle mode, and fits him with some kind of prototype prosthetic leg. Of course, it would help even more if the issue of Sam not repaying Bee or relying too much on him for protection was present throughout the movie.