At home, put him in his room. This is different than time out. For time out, they have to sit until you tell them to get up. For this, you put him in his room, don't even close the door, calmly tell him he can come out when he is all done screaming, then walk out. If he follows you, pick him up and plop him back down in his room over and over again until he is either no longer screaming or will stay when you walk out. Usually, they will eventually stay and just keep screaming even more out of frustration. But they eventually get the picture. My son is 3.5 and I still have to do this occasioanlly, but now I can say if you can't stop yelling then you will need to do it in your room because no one out here wants to hear you yell. Sometimes he will calm down and start playing and forget all about whatever he was yelling about, sometimes he will calm down and walk out on his own and see things my way. Sometimes he will come out and ask again for whatever I had originally said no about to test me and I stand my ground-- he is either OK with it or he starts screaming again, in which case he has to go back in his room.
If you are in public, I know everyone says you have to leave and it's not always easy to do that, but do it if you can. If it's at someone's house or someplace where I can just walk away for a minute, I take him over to the side and hold him and say, "Sorry, but you may not have a cookie. We're going to stay over here until you can calm down."
The key is for YOU to stay calm and not feed into it by getting visibly upset or he wins. I don't agree with people who say to just ignore it. He needs to understand that it is not acceptable and will not be tolerated, and that he will be removed from the common area every time he does it. Take away his audience and give him a place to calm down. Just matter of factly tell him that he is not going to get whatever it is that he is crying for, period. Don't keep sayiing a lot, and most importantly, just say it calmly.