To reliably steal, you need to get out of the guard's line of sight
and stay there for a while.
Your chance to be caught at thievery seems to correlate to the guard's awareness level of you. You can easily see your chance to fail as a percentage next to the item's name when you're about to loot it.
If the guard is in sight, you can also see how aware he is of you by entering stealth mode and looking at the icon over his head.
This means that a guard may not even be in the same room as you, but may notice you steal, simply because his "awareness" from when he last saw you hasn't worn off yet. His awareness will drop over time as you stay out of his sight. My experience is that the awareness level seems to drop for different guards at different rates... it was almost instantaneous for the guard in the alchemist's shop in Gorhart, but took longer for the guard in Shieldring Keep.