The greenhouse effect works by trapping heat within the Earth's atmosphere. Gases in the atmosphere, such as carbon dioxide and methane, trap heat from the Sun, preventing it from escaping back into space.