In Return of the Jedi C3PO states that it is against his programming to impersonate a deity, but he does it anyway when the others tell him to. How is it possible for him to disobey his programming?
Because it wasn't a hard prohibition. More of a protocol guideline.
Threepio pulled himself up to his full height, and spoke with unrelenting decorum. “I beg your pardon, Captain Solo, but that wouldn’t be proper.”
“Proper!?” Solo roared. He always knew this pompous droid was going to go too far with him one day—and this might well be the day.
“It’s against my programming to impersonate a deity,” he replied to Solo, as if nothing so obvious needed explanation.
The godhood actually went to his golden, insufferable head
Behind the captives, Threepio, borne on a litter—rough-hewn of branches in the shape of a chair—was carried high upon the shoulders of the lowly Ewoks. Like a royal potentate, he perused the mighty forest through which they carried him—the magnificent lavender sunset glowing between the vinery, the exotic flowers starting to close, the ageless trees, the glistening ferns—and knew that no one before him had ever appreciated these things in just precisely the manner he was now. No one else had his sensors, his circuits, his programs, his memory banks—and so in some real way, he was the creator of this little universe, its images, and colors.
And it was good.
and later, when R2D2 got done zapping the Ewok:
Artoo scooted over directly in front of the golden droid, and began beeping a vehement tirade. “Wreee op doo rhee vrrr gk gdk dk whoo dop dhop vree doo dweet …”
This outburst miffed Threepio substantially. With a haughty tilt he sat up straight in his throne. “That’s no way to speak to someone in my position.”
He figured out - with Luke's prompting - that things got serious.
Han was actually about to be roasted alive - which would trigger quite a different weight of priority in his programming (think Asimov's 1st law) vs. merely "Han and company are captured".
Luke was afraid the situation was well on its way to getting out of control. He called with the barest hint of impatience to his faithful droid. “Threepio, I think it’s time you spoke on our behalf.”
Threepio — rather ungraciously, actually — turned to the assemblage of fuzzy creatures and made a short speech, pointing from time to time to his friends tied to the stakes.
Luke gave him a direct order, impatiently. C-3PO probably didn't want to upset Luke much, what with being a Jedi Knight with a laser sword.
“Tell them!” Luke ordered, uncharacteristically raising his voice. There were times when Threepio could test even the patience of a Jedi.
The interpreter-droid turned to the large audience, and spoke with great dignity. “Eemeeblee screesh oahr aish sh sheestee meep eep eep.”
And, strictly speaking, what Luke ordered him to say wasn't "impersonating a deity", from a certain point of view.
Merely impersonate someone who could use magic.
Before he could speak up with his plan, though, Luke chimed in. “Threepio, tell them if they don’t do as you wish, you’ll become angry and use your magic.”
“But Master Luke, what magic?” the droid protested. “I couldn’t—”
All quotes are from James Kahn's novelization of ROTJ, Chapters 5 and 6.