The short answer is because of European colonialism. Flags have been around as decorative objects for millennia, but the practice of having a single standardized rectangular flag design that stands for a country, its government, its people, and just sort of everything about it comes out of early modern Europe.
It was originally a naval practice. Ships flew flags to indicate what country they were from. A rectangular flag catches the wind better than any other shape, so that tended to be the shape that countries would use. Pretty soon the idea of "a flag that represents a country a sea" evolved into "a flag that represents a country in general", and rectangular flags became more common on land.
European imperialism brought these kinds of "national flags" to areas that didn't have local traditions of flag use, like Sub-Saharan Africa and the Americas. When states in these parts of the world became independent, they would adopt the same types of symbols that other independent states had (meaning rectangular flags, national anthems, coats of arms, stamps, and so on). Even countries that weren't directly colonized were influenced by European practice. Japan, China, and Siam all adopted rectangular flags in the nineteenth century as their trade and diplomatic interaction with Europe increased.
Nepal is the last survivor of an entirely different tradition of flag design based in South Asia and Iran. These flags tended to be more like royal standards, special one-of-a-kind designs that represented the authority of a monarch and weren't really for use by the general public. Because of this they didn't have to be easy to manufacture or practical to use at sea. That meant they were ornate, used a wide range of colours, and came in all sorts of shapes. Triangular pennants were particularly popular.
Nepal's kings flew a triangular pennant with the moon on it. But Nepal also had a line of hereditary Prime Ministers, and they had their own flag: a triangular pennant with the sun on it. The two flags were commonly flown together and eventually they just kind of merged into one. Many of India's princely states flew non-rectangular flags like Nepal's, but they were all banned after independence in 1947.
Don't forget the State Flag of Ohio, whose design was inspired by Civil War cavalry pennants (or, more appropriately, burgees).
You should note that there are many non-rectangular flags. Most northern European navies, for example, use swallowtail flags.