We study the impact of the wetting properties on the immiscible displacement of a viscous fluid in disordered porous media. We present a novel pore-scale model that captures wettability and dynamic effects, including the spatiotemporal nonlocality associated with interface readjustments. Our simulations show that increasing the wettability of the invading fluid (the contact angle) promotes cooperative pore filling that stabilizes the invasion and that this effect is suppressed as the flow rate increases, due to viscous instabilities. We use scaling analysis to derive two dimensionless numbers that predict the mode of displacement. By elucidating the underlying mechanisms, we explain classical yet intriguing experimental observations. These insights could be used to improve technologies such as hydraulic fracturing, CO2 geosequestration, and microfluidics.