Question: There are recognized countries that are members of UN.  

There are recognized countries that are not members of UN.  

There are legal countries that are not widely recognized. 

So, what is the relation between "recognition" and "legality" of a country"?

Answer: 
One of the definitions of **"legality"** basically means if a country declared its independence legally - maybe through a peace treaty, a referendum, etc.

It isn't that important in the foreign affairs aspect since diplomatic ties are based on "recognition". Ultimately, a country needs to be recognised to be able to gain membership in inter-governmental organisations or to travel freely to other nations.

---

**"[Diplomatic] Recognition"** as defined in Wikipedia:

> 
> Diplomatic recognition in international law is a unilateral political act with domestic and international legal consequences, whereby **a state acknowledges an act or status of another state or government in control of a state** (may be also a recognized state).
> 
> 
> 

The UN also describes this [on their website](http://www.un.org/en/sections/member-states/about-un-membership/index.html):

> 
> **How does a new State or Government obtain recognition by the United Nations?**
> 
> 
> The recognition of a new State or Government is an act that only other States and Governments may grant or withhold. It generally **implies readiness to assume diplomatic relations**. The **United Nations is neither a State nor a Government, and therefore does not possess any authority to recognize either a State or a Government**. As an organization of independent States, it may admit a new State to its membership or accept the credentials of the representatives of a new Government.
> 
> 
> 

This is more important in the diplomatic aspect as countries need to be recognised by a number of other countries in order to be a member of the United Nations.

An example would be Kosovo, which is recognised by 57% of UN member states but is still not a member of the UN.

---

In conclusion, "legality" basically means the legality of the existent of a state, but it doesn't really hold much meaning or value on the international stage while recognition means whether other countries accept you as a sovereign nation/ state or do they count you as territory of another country.
LEGALITY AND RECOGNITION ARE TWO COMPLETELY DIFFERENT THINGS  

(Neither of them is a "necessary" or a "sufficient" condition for the other one)

Recognition is completely a political notion/act (as stated by International Court of Justice, Kosovo 2010 decision) and has nothing to do with legality.  

So, even if no country recognizes a country, this has nothing to do with the legality of that country.

Then-President of the International Court of Justice (ICJ) Hisashi OWADA (2010): ["International law contains no "prohibition on declarations of independence."](http://www.bbc.com/news/world-europe-10730573) 

The International Court of Justice (ICJ) (2010): "while the declaration may not have been illegal, the issue of RECOGNITION was a POLITICAL one".  

(in [OSHISANYA 2016, An Almanac of Contemporary and Comperative Judicial Restatement](https://books.google.com.tr/books?id=xMvOBAAAQBAJ), p.64)

Hence, "recognition is a political action/matter, not a legal matter".  

That is to say, “being recognized/not recognized does not affect legality/illegality of a country”.

That said, as far as "legality" and "recognition" are considered, the existence of either of them helps the existence of the other.