Surprisingly, gable vents do both, let cool air in if available and more importantly let hot air out of the loft or heated attic space.
Most all gable vents are installed to the upper most part of the gable walls. This is nearest to the hot air that needs to be expelled. Louvers or slats are angled downward for water protection so air needs heated before escaping through.
Hot air attracts cooler air. So imagine a tornado swirling in the attic of hot air grabbing up cool air. This swirling motion isn't vertical though, it's horizontal. The vortex rolls over at the highest and hottest point of the attic just under the rafters. This allows the cooler air lower towards the attic floor to be swooped up slowly. As this hot air reaches a certian temperature, it will locate the gable vent and exit. The path of least resistance.
Now as far as when a gable vent allows cool air into the attic... The sunrise will only heat one side of the home as it rises in the morning. This means the vent neasrest the sunrise will start expelling heat first. Then as the sun is directly overhead, both or all gable vents should be pushing hot air out. Later that day, as the sun sets, the heat will be on the other side of the house. The vent nearest the sunrise will be cooler and now allow some cooler air to enter and assist in cooling the attic. Most attics should have more than one exhaust vent and many soffit or cornice vents to bring in cool air for the gable vents to exhaust hot air easier.
Cooler attics will last longer and thank you by extending your roof life by many years. An attic over 130 degrees is cooking itself making rafters brittle and knocking a 30 year roof down to about 10 years. By monetary percentage, gable vents are cheap in comparrison to replacing an entire roof.
The image above shows how cool air enters the overhang with cornice or soffit vents. Then rises as it heats up. At a certain temperture, the air will begin to swirl like a horizontal whirlwind. This cannot be seen with the naked eye but can be felt when placing a hand on the attic floor versus on a rafter up high. Some believe ridge vents do a better job than gable vents and if the attic is closed off where there can be no gable vent access, then yes, use a roof vent or ridge vent. Otherwise, the ridge vent is limited to only starting venting over 130 degrees in most cases. At this point the ridge vent cannot vent hot air quickly enough to keep the attic cooled. Overheated attics are a major fire hazzard in the summer in most climates. It could be an overheated wire to spark or a cardboard box with Christmas decorations or it may be as simple as a rafter dried to the point of kindling. Dry and hot items do not usually take much to ignite.