Your question includes three different concepts: UN membership, recognition, and legality. I will explain each one in turn as well as drawing distinctions or relationships between each thing.
United Nations Membership
The rules for joining the United Nations can be found here. The most relevant aspects are:
- States must submit an application. The application affirms their willingness to adhere to the Charter of the United Nations.
- The Security Council will approve or disapprove of their application. There are no rules or guidance for this - it is strictly up to the Council.
- If the Council approves the application, it moves to the General Assembly for a vote.
Although there are no formal rules for when a state should be admitted or not, the Charter does say this:
Membership in the United Nations is open to all other peace-loving states which accept the obligations contained in the present Charter and, in the judgment of the Organization, are able and willing to carry out these obligations. Source: Chapter II Art.4
United Nations membership just means that a state has been approved to be a member of the United Nations. They have agreed to adhere to the UN charter and have the approval of the Security Council and General Assembly. There is no notion of "legality" or "recognition", just the idea of being a state.
What is a state? (Legality)
The generally-cited legal description of a state comes from the Montevideo Convention.
Article I defines a state:
The state as a person of international law should possess the following qualifications:
a ) a permanent population;
b ) a defined territory;
c ) government; and
d) capacity to enter into relations with the other states.
This is the closest you will come to a concept of "legality". The Convention expressly says that a state may be legal and not be recognized:
The political existence of the state is independent of recognition by the other states (Source: Article 3)
Recognition
No single entity decides when a state is a state, and no single entity decides when a state is recognized. Rather, each state decides when to recognize a state. There is a distinct one-to-one relationship here: if something is a state, than every other state is required to recognize it (so there are no unrecognized states except in the short-term), and everything recognized as a state is a state (so there are no incorrectly recognized states) (See this analysis from the Yale Law Review).
Effectively, this means that any legal state must be recognized by other nations, and also that all members of the United Nations are recognized states.
The wikipedia article nicely sums up diplomatic recognition, which is mostly a matter of custom rather than formal law. Recognition is unilateral (Country A chooses to recognize Country B, this does not mean that Country B recognize or does not recognize Country A).
De facto recognition occurs from extra-legal acts. For example, if the President of the United States visits with heads of a country in an official sense, that is effective recognition of that state. Recognition can also occur through legal channels. For example, when a United Nations member votes in favor of admitting a state to the United Nations, they are offering a legal admittance that the state is (in fact) a state.
Quick Summation
UN membership is only open to states. UN membership requires a vote from the General Assembly, and voting to admit a state is legal recognition of that state. So all members of the United Nations are both legal and recognized (by the majority of UN member nations) states.
Legal states may theoretically be unrecognized, although every state has an obligation to recognize a legal state. In practice, the evidence that a state is a legal state is recognition, so (in practice) there are no unrecognized legal states.
Recognized states are always legal states. However, not all recognized states may be members of the United Nations. It's possible that a recognized state can choose not to accept the United Nations charter or seek membership, or that it will fail to be admitted.