Wifi has priority over everything. Mainly because its usually the fastest, you don't get charged for data usage by your carrier, and it uses less power in some ways. However, if there is no wifi available, it just uses your carrier's data connection. Its pretty simple really.
The only time I see this hasn't been the case is that MMS's always go over your carrier's data network regardless of wifi status.