Can you tell me if it is a dwarf hamster or a syrian hamster? (Is it larger or smaller than your hand?)
(small ones) can be very cage aggressive if they live in too small of a cage. Cages sold in typical pet stores are mostly way too small for hamsters, unless you add on to them. So, if your ham has lived in too tiny of a home, it can become territorial and aggressively bite any "intruders."
For the future, get ready for bites when you go to pick him up. Start to notice his behavior RIGHT BEFORE he goes in for a bite. Learn these behaviors, and then learn to blow on his head, forcefully (although not TOO forcefully) so it distracts him from his immediate action: biting you. Keep blowing to keep him distracted.. he might either tire of it, or keep going. It's... not a science
But sometimes when you distract him by blowing on his head, you can scoop him up with the other hand.
Always always have treats on had to reward him. Never punish him (the blowing isn't a punishment, just a mild irritation), especially after biting you. Hamsters, like dogs, do not understand that they are being punished for a previous action. Instead, you have only the exact moment to correct a behavior.
If you need serious taming, then find a treat you know your hamster LOVES. Only give him this treat when you have successfully picked him up with out any bites. Again, like dogs, a delayed reward means nothing. You have to reward him for the good behavior RIGHT when it happens. This can be tricky. But overtime if done correctly, your hamster will stop biting you, because he will somehow know, if he lets you pick him up with no bites, he will get his favorite treat.