You need two things, and neither works without the other.
This can be the spell itself or an enchanted item. An item might carry Soul Trap as a castable power, or it might be a weapon that casts the effect on whatever it strikes. Either way, the target has to die before the spell expires, or nothing is captured.
Gems come in different sizes, and bigger souls need bigger stones. An Ogre requires a Greater Soul Stone; a Savage Ogre needs a Grand Soul Stone.
The game splits souls into two kinds. Sentient, intelligent races have a black soul, while beasts, Daedra and unintelligent creatures such as goblins have a white soul. Only Black Soul Gems — and Azura's Star — can hold the soul of a human, elf, orc, khajiit or argonian. The sentient Daedra, the Dremora, and the Shivering Isles races, the Aureals and Mazken, count as black souls too.
Enchanting with a black soul is also the only way to make constant-strength enchantments, which is why they are worth the effort of hunting.