Welcome to the Apple Support Communities
It depends on the TV, because there are some TVs which switchs automatically to HDMI or VGA when they detect a device connected, and there are others in which you have to put the TV in HDMI mode. Also, note that your Apple TV must be turned on in order to be detected by the iMac and iPad as available to activate AirPlay Mirroring or AirPlay
You are referring to HDMI Consumer Electronics Control (HDMI CEC) protocol for automatic control of the compatible equipment. It is performed through the HDMI cable (for example a device connected to a TV can change the video source on the TV to start broadcasting its feed). Unfortunately current version of Apple TV (3rd gen, Rev A) does not support this protocol. Therefore you cannot achieve what you describe automatically. However, you can do everything manually, using remote controls of your home system, TV, etc.