Geneva was the headquarters of League of Nations even though it did not participate in WWI. It has the UNOG from 1946 though Switzerland has not joined UN till 2002. It was the headquarters of OPEC till 1965 though Switzerland is not one of the oil producing countries. In addition to the above, Geneva is the headquarters for a number of International organizations. Why do many International organizations have their headquarters in Geneva?
Imagine your home country got invaded by the fictional superpower of Elbonia. The Elbonian soldiers burned down your house, abducted and/or killed several of your friends and relatives and kicked your dog. Now a self-proclaimed humanitarian help organization with their headquarter in Elbonia comes along and says: "Yeah, very bad what our government did to you, but we are here to help you. We can bring you to a refugee camp where you receive totally non-poisoned food and completely non-experimental medical care and will certainly not be tortured for information. You just need to trust us". Would you come along?
Switzerland is a country which always had a policy of neutrality and non-involvement in international affairs. For almost 500 years and counting Switzerland refused membership in any military alliance, and is also weary of any civil treaties. It is not an EU member despite its geographic position right in the center of Europe. Until 2002 Switzerland wasn't even a member of the United Nations.
That means an organization based in Switzerland can deny that their actions are influenced by the interests of the government of the country they reside in.
As the second largest city in the country, Geneva provides a good infrastructure for an international organization.
Historically, Geneva has the condition of being in Switzerland, which is a country where international treaties are signed in the modern history. After the Napoleonic wars and joined the Helvetic Confederation, here celebrated most of the international treaties, which have huge impact in the Western World; Starting in 1863 when Henri Dunant founded the International Red Cross (ICRC) and still having an office in the city.
They were local to the first Geneva Convention, the Hague Conventions of 1899 and 1907, the Third Geneva Convention of 1929 among others which are historically and juridically important. Also, Geneva was the place where the League of Nations was established; when the League didn't work, they used the Geneva infrastructure to build the United Nations in 1946.
Interesting fact is that most of the recognized international NGOs has office over there and where it held the World Economic Forum or tweet it #WEF for more information.
First and foremost, Switzerland has a long tradition of strict neutrality, which helps diplomacy. Plus, it has gorgeous landscapes, right in the middle of Europe (transportations, of course, weren't as efficient as today). It's also French speaking and French was the language of the elite and diplomacy in the 19th century. Many aristocrats went to Switzerland on holiday or sent their kids there to private schools.
In general, it's also difficult for large and powerful countries to agree to grant the seat or leadership of any international organisation to each other (the UN in New York being one major exception). See for example the citizenship of the UN secretaries general, the seat of the European Union, etc.
Switzerland was therefore able to attract some of the earliest modern international organisations, like the Universal Postal Union and International Telecommunication Union. Furthermore, the Red Cross movement was initiated by a citizen of Geneva.
Add to that the fact that Europe was still much more influential than it is today and that Switzerland did not participate in the First World War and you can see why it was well-positioned to attract the seat of the League of Nations (incidentally, another small non-belligerant, the Netherlands, also hosts a number of international organisations). And many of the organisations based in Geneva are now part of the “UN system”, which succeeded the League of Nations.
The current situation kind of follows from this history. There is no obvious reasons why international organisations would choose Geneva today but the infrastructure, workforce, etc. is in place and it would be very costly to relocate all this.
And Switzerland has always been keen on protecting this heritage and punching above its weight diplomatically. It frequently offers itself as a mediator between warring countries, acting as a protecting power and hosting foreign interest sections in Swiss embassies everywhere. (Here as well there is another small neutral country playing a similar role: Sweden.)
The same dynamic would also seem to apply to OPEC. It couldn't very well be hosted in one of the major production countries and many of the smaller ones are unstable. In a continuation of its 19th century tradition, the Geneva area is still a common destination for rich and powerful people, e.g. from the Middle East. Many heads of state have villas on the lake shore or go to Geneva hospital to get medical treatment. I don't quite know why OPEC moved but its new seat (Vienna) is similar in many ways (secondary UN seat, small neutral country – albeit for a much shorter time than Switzerland).