We explore the effects of an outer stably stratified coronal envelope on rotating turbulent convection, differential rotation, and large-scale dynamo action in spherical wedge models of the Sun. We solve the compressible magnetohydrodynamic equations in a two-layer model with unstable stratification below the surface, representing the convection zone, and a stably stratified coronal envelope above. The interface represents a free surface. We compare our model to models that have no coronal envelope. The presence of a coronal envelope is found to modify the Reynolds stress and the $Lambda$ effect resulting in a weaker and non-cylindrical differential rotation. This is related to the reduced latitudinal temperature variations that are caused by and dependent on the angular velocity. Some simulations develop a near-surface shear layer that we can relate to a sign change in the meridional Reynolds stress term in the thermal wind balance equation. Furthermore, the presence of a free surface changes the magnetic field evolution since the toroidal field is concentrated closer to the surface. In all simulations, however, the migration direction of the mean magnetic field can be explained by the Parker--Yoshimura rule, which is consistent with earlier findings. A realistic treatment of the upper boundary in spherical dynamo simulations is crucial for the dynamics of the flow and magnetic field evolution.