Why do all the airplanes have to retract their landing gear, once they reach some specific height? Why can't they simply leave their landing gear deployed all through the flight?
In the early days of aviation it was simply easier to have the gear fixed. The landing gear adds drag, but so does a second wing and wire braces. Airplanes generally did not fly very fast or far, so the drag from the landing gear (and that other stuff) was not a big issue. The first design with retractable gear dates to 1911. Since then, retractable gear has been a design decision each airplane must make.
Retracting the gear into the airplane allows for a cleaner form, which reduces drag. However, this is at the expense of added weight. The retract system, which is usually hydraulic, must be added, and the plane must be designed to make room for the gear somewhere at least mostly inside the aircraft. The plane must also be designed to handle situations where some or all of the gear does not extend properly. For planes that need to be fast and/or efficient, retractable gear is worth the reduction in drag.
The additional drag has to be supported by the structure as well. Aircraft with retractable gear will have a maximum speed at which the gear can be extended. Even gear with proper fairings would add significant drag at the speeds airliners fly at. This would be much more critical at supersonic speeds. In these cases the weight of reinforcing the gear could be even higher than the weight of the retracting system.
Particularly smaller general aviation planes tend to not have retractable gear. The retract system adds extra weight and complexity (and therefore cost), which will be fairly significant in a small plane. Fixed gear is simple and can be designed to minimize drag as much as possible. These planes do not typically need to fly fast or have large range, so the added drag is less of an issue. Other small planes do have retractable gear, which will allow for greater speed and range. The PA-32 type shown in Dave's answer was later produced in a retractable gear version as well.
Short answer: It allows them to fly faster and further.
Long answer: Hapag-Lloyd Flight 3378 demonstrated this impressively on July 12, 2000. Destined for Hannover, they did not retract the gear after take-off in Chania, Crete. The fuel was sufficient for the planned distance plus reserves, but the extended gear increased fuel consumption so much that they ran out of fuel while approaching their diversion airfield near Vienna. Fuel consumption enroute was twice as high as it would had been with the gear retracted.
Drag is composed of a lift-related component (induced drag, blue line below) and a constant component (zero-lift drag, red line below), and both depend on the dynamic pressure, which is the product of air density and the square of airspeed. While the lift-related component goes up with reduced dynamic pressure, the constant component will go up with increased dynamic pressure. Thus, extending the gear will increase the constant drag component, and while flying slower helps to reduce its contribution, it will drive up the lift-related drag component. In the end, the drag will be higher at all speeds.
Typical drag contributions over speed for a glider. The physics for airliners is the same, only the numbers are bigger. In cruise, all airliners try to fly as close to the minimum drag as possible.
The only reason for a fixed gear are cost, weight and simplicity. Performance will always suffer from it.
Because its a drag to leave it down(pun intended). Some smaller GA planes (and big planes too) can extend the landing gear through a wide range of their operating speeds and use them as a pretty effective speed break. The DA42 comes to mind in this regard.
For what its worth there are many general aviation planes that have fixed gear.
Retracting gear in a small plane has always been an interesting topic. Some older piston singles have manual retracting gear like the early Mooney M20's which used a Johnson bar to retract the gear. This some what dated system is, by some very sought after for its simplicity.
(the big silver bar in the middle is the gear lever)
Folding gear also has little to do with aircraft size. The Mooney M-18 "Mite" which is a tiny plane by anyones standards had retractable gear (also Johnson bar operated).
One way to reduce drag on a fixed gear plane is through the use of gear fairings. while they do help with speed they can also cause an issue if the plane touches down hard. In some cases (if the wheel is improperly inflated) a hard touchdown or bounce will cause the wheel to balloon out while also spinning which could chafe or seriously damage the fairing.
Why do all the airplanes have to retract their landing gear, once they reach some specific height?
Why can't they simply leave their landing gear deployed all through the flight?
Both questions are false on their premise:
All airplanes do not have to retract their gear; many planes are fixed-gear, and cannot retract gear. Some planes retract their gear.
Planes can leave their gear deployed all through flight, provided they remain slow enough that the extra airflow drag doesn't damage the airplane and landing gear.
Planes that have retractable gear generally retract their gear for improved performance. Not having wheels and struts hanging down streamlines the plane, improves fuel efficiency, enables them to fly faster and higher, and makes for a quieter, more comfortable ride inside the cabin. So, retracting gear is generally desirable.
But, as your question suggested, it is not required, and it is not based on a specific height. (if anything, it is based on an airspeed)
Simply retractable landing gear is made so as to reduce extra drag which created by the fixed landing gear and hence heavier than air aircraft(airplane) can improve in speed and travel at high speed at higher altitude