I've always wondered why the UN considered Israel a legitimate country?
The history is very clear, until 70 years ago they didn't exist.
The United Nations recognize the state of Israel because the decision to form the state was made by the United Nations themselves in 1947. That means it is to be expected that the UN would stand by their decision.
To give more historical context: Palestine was conquered by the United Kingdom from the Ottoman Empire during World War I. The United Kingdom declared their intention to form a Jewish state on the territory back in 1917 with the Balfour Declaration. During the raise of national-socialism and World War II, many Jews fled to Palestine. In 1947, 33% of the population of Palestine was already Jewish.
Whether or not all the actions of the Israelian government between 1947 and today are within the bounds of international law or general ethics is a matter of very heated debate. But no matter where you stand on the debate about the Palestine/Israel conflict, there is little ground to deny that Israel is a state according to all generally accepted definitions of statehood.
If we tried to argue about who was around first, that could go on for a very long time. A large number of people in Israel trace their lineage to the region, going back hundreds, if not thousands of years. Palestine itself is a term given to the region by the Romans who displaced the "native people."
The OP's position is clear on this issue, but it is not really valid. The OP only looks at the last century or so, and fails to take into account any philosophical or political theory on legitimacy. What it boils down to is that the state of Israel is legitimate because it exists. Those who are currently using a piece of land have a legitimate "right" to the land because it would take force in order to remove them.
It is difficult to capture the full argument necessary to justify this position here, however I have written about the nature of rights in detail. Basically, a right is that which is voluntarily enjoyed, in the absence of others, and which does not intrude on the rights of any subset of the group (Rights, an Update).
This idea of rights implies in a fairly direct way that there is no true ownership of a piece of land. Yes; removing someone's current access to the land would be a violation of that person's rights, but saying that a person has a legitimate right to land because of ancestry is not reasonable (The Myth of Private Property).
The alternative would be to somehow figure out who was really there first, something that is fairly difficult, who should have received the title with each successive generation, which is again difficult, and so on. It would also mean that no matter how many years pass, anyone who traces original title would be able to throw someone off of the land that is currently being used.
Let us just apply that idea, for a moment, in a thought experiment. If we demanded that Israel is not legitimate because it was "taken" from someone else, then we would have to accept that the United States is not legitimate for the same reason. But we cannot just give the United States to some random tribe. It would have to be given to the "rightful heirs" of the first person from which it was taken. That may not even be the tribe from which the Europeans took the land because that tribe may have taken it from someone else.
So not only would it be unrealistic to even figure out who has rightful title, it would basically mean that land would have to be give up in any case where title had, at any given time, been taken by force. That would mean figuring out what tribal descendants have rightful title in the United States. It would mean that most of Europe would have to be reworked based on all the land grabs that had taken place. Just think about how many times England had been taken over.
Furthermore, as Philipp stated. Israel was actually created by the founding nations of the UN, which means that it would be hard for any member nation, especially any member nation that was a part of the decision to create the modern state of Israel, to argue that the state is not legitimate.
The gist of your question is rooted in half truths.
The history is very clear, until 70 years ago they didn't exist.
This is a half truth. The land, Israel, did exist. The Independent Nation of Israel had, at the time, essentially never existed, or maybe for a time 3,000 years ago. 70 plus years ago it was a colony of England, and before that a colony of Turkey and before that, there were the Crusades, and before that, part of the Byzantine Empire, and before that Rome, and I may have missed two or three in my very brief history lesson.
Point is, land that is subject to a distant nation that becomes country when the last of the long line of possessors leaves, that's clearly a grey area. OK, now that the colonizers have left - what becomes of the colony?
The UK, when they pulled out, not long after WW2, they divided Israel into two parts, Muslims on the East and Jews on the West. That was the last colonizer's solution before pulling out, so without question, that gives the nation of Israel some validity.
They invaded land that doesn't belong to them and killed most of the population
I don't think this is entirely true. Yes, Israel has grown since the English drew the border on a map and left, and a case could be made that they took land that didn't belong to them.
I don't think a case could be made that the "killed most of the population". There were casualties on both sides. You should also consider the Purge.
While many Jews did migrate, in some cases, illegally crossing borders to get to Israel following WW2 and there was a lot of uncertainty on whether the independent state of Israel should be recognized. The expulsion of Jews from across the middle east (many went to Israel), arguably legitimized the state of Israel. If hundreds of thousands of Jews lost their land and their homes across the middle east, that makes a strong case for the legitimacy of the fledgling Jewish state that takes them in.
I'm one of the people who takes neither side in this conflict, simply because I don't feel a strong bond to ether side and because I have sympathy for both sides. This one is a pickle. There are no easy answers, and likewise, in my opinion, there's no true morally correct answer either.
But based on your question, you seem to be influenced by half truths and false arguments, with little regard for history and I'll add to that, you've not done sufficient research before asking your quesiton. At least read a few points of view from either side before you make up your mind.
But it's a fairly common question so I thought I'd answer it anyway.
Your premises are incorrect.
The UN considered Israel a legitimate country because it was established on the basis of a UN resolution.
Israel did not invade the land, the Israelis bought most of the land they settled. They did not kill most of the population, if they were to have done so we would not have today the Palestinian refugee situation, the 20% Arab citizens of Israel, and the several millions in Gaza and in the West Bank.
When the question is about international relations and UN actions, You should not look for logical reasons; instead, You should look for benefits.
Israel is considered legitimate country because Powerful countries, (or some effective people in those countries), supported the idea. Why? Because of benefits and leverage that Powerful countries earn, (or benefits and leverage some effective people in that countries earn). What benefits? This can be a separate question.