It should do this automatically. If you refer to when you put your phone to sleep and 3g tries to turn on, head to settings > wireless & networks> Wifi settings> menu> Advanced > and turn Wifi sleep policy to "Never".
From what I've read, people get better battery life if they set "enable always on mobile" to off. This just shuts the 3G off when the phone is asleep. If you're still on wifi you get all your emails and syncs.
I just added a shortcut to manually switch my 3G off when on wife since I'm on wifi 90% of the day. I'm just lazy and don't want to have to switch 3G off/on every time I'm on/off wifi.
I know that when it's on wifi the 3G should auto shut off but I think it still periodically turns on for whatever reason.