First off, this will probably work best on a new Red or Blue file.
Make sure you did not battle the Youngster with a Level 17 Slowpoke, on the way to Bill's house. If you have, the code won't work.
Also make sure you did not battle the Gambler with two Poliwags and a Poliwhirl. He is facing the Underground Path toward Celadon city on Route 8. If you have, the code won't work either.
You need to be able to use Fly out of battle.
Stock up on many items to catch it.
Go to the Gambler on Route 8. Save AT the door he is facing. Don't make him battle you. SAVE HERE, as this part may get tricky. Now, step one step towards him, and PRESS START quickly. If you did it right, it will pop up, but he won't battle you. (This should not work for any other trainer) Now Fly to Cerulean City. If you've done this right, after you use Fly, he will get the "!" over his head.
Note: You cannot use START, A, or the B buttons.
Note: Do NOT save anywhere after this. When the buttons work again, saving will ruin the Mew code, and not allow you to get the Mew ever again. Keep your saved file from before you battled the Gambler.
Now walk to the Youngster, and when you battle him, make sure he WALKS TO YOU. Don't go directly in front of him. Beat him, and Fly to Lavender Town or Saffron City.
Now if you went to Lavender, go to the left of town, and start to enter that narrow passageway. If you went to Saffron, go right, to where the guard lets you past if you give him a drink. Either way, the Start menu will pop up, but do not save here! Press B. Mew will appear. This is your ONLY chance, unless you go to your save file before the Gambler. It's a Mew! Enjoy.