When did we realize our past was worth saving? Does a certain amount of time generally pass before discoveries begin?
Museums have been around for ages. The word "museum" comes from the greek "museion", if memory serves me right, a temple dedicated to the muses for contemplation and study of the arts. Museums have been there forever, under different ideals: monasteries in the middle ages were known for maintaining original ancient greek and roman artefacts; Cesare Ripa's Iconology—very influential book for artists in the late 1500s—was made posible by the sheer amount of art collections noble people had in their private galleries.
However, our modern idea of a museum comes from the late 1600s, when wealthy noblemen started to collect all sorts of different memorabilia and displaying them in what was called a "cabinet of curiosity". These small rooms and cabinets included all different sorts of artefacts (some were more consistent than others) but they all had a somewhat scientific study to it. Even tough these "cabinets" in no way fit to be compared to present-day museums, they have one thing in common: a space that was open for a specific amount of time and allowed all different types of people to muse over a collection, be that for leisure (contemplation, admiration, showing-off) or for study (taxidermy, ethnography history, geography).
Museums as we know them today really started in 1789 with the French Revolution. Education was made equally accesible, so national galleries where state-owned collections could be displayed for nation-wide contemplation were seen as necessary. This process was copied by many nations across Europe, even if, unlike France, they stuck to the standard monarchic regime, but somehow become a little more liberal throughout time. After the monarch and the noble families (who kept most of their belongings), the clergy owned the biggest collections, so by the time the separation of state and church became a reality across the continent—and seeing that the clergy also controlled education—, all of the church-owned artefacts were added to public museums. This presented people with a philosophical dilemma because church-owned objects were original commissioned as objects for religious worship. The museum was not a space of worship, so people had to "learn" how to navigate the museum under this notion that these were no longer objects of worship but for contemplation and study.
History, as far as art galleries and museums go, played only a little role in the first place, as they develop from separe interests. Art history, for instance, was "born" out of the need for a classification of ancient greek art. So, throughout most of the 1700 to 1800s, art history was really more inclined towards archeology. It was only when post-revolution Paris become the cultural hub of the planet that art study and art practice took onto a different approach (even tough the Academy had existed since the 1600s). Yet, even then, art criticism navigated the realms of beauty and aesthetics, with no concerns to history. Simply put, art had to be preserved and studied because it was beautiful and not because it spoke about times immemorial. That is a concern that will only rise in the late 1800s and would "triumph" in the 1900s.
A lot more is there to be said, but for conciseness, some abridging had to be done.
Jürgen Osterhammel gives a very good explanation of this in his The Transformation Of The World (Die Verwandlung der Welt). Museums started in the end of the XVIIIth Century, mostly as cabinets of curiosities, but really developed in the XIXth century. Based on his observations, I would say that museums were created around the world with the help of a few concurring phenomenons. (1) The development of nationalism and new conceptions of citizenship which made it important to educate the people with public institutions, and also to showcase national treasures of various kind (including contemporary art) to build a narrative of nationhood (2) the development of new conceptions of science and rationality which permitted new classes of specialists to act as "curators" (art historians, naturalists, ethnographers and so on) of the knowledge to be shared to the masses (3) the development of colonialism which made representation both of Western rationality and its domination over Oriental or "primitive" societies important, this appropriation of foreign artifacts could then be used both as a tool for national Bildung and as a proof of racial and national superiority (4) the reaction to the development of colonialism and European hegemony, that made the museum a tool used worldwide for building a counter-narrative of national emancipation or greatness (5) especially in the United States, the development of industrial capitalism and the raise of a new bourgeoisie, that made private investment in public institutions a way to obtain symbolic power.