Is it legal?

Yes and no. The precise laws and requirements vary from country to country.
== Europe ==
Country 	Status 	Statistics 	Map	Sources
Albania 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Andorra 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Armenia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Austria 	Legal under restrictive conditions, homeschooling is allowed as long as the instruction is at least equal to that of the state school. 	Unknown 	███	1 2
Azerbaijan 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Belarus 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Belgium 	Legal under restrictive conditions, only legal for parents that have a teaching certificate. 	500/1million 2005-2006 	███	1 2 3
Bosnia and Herz.	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Bulgaria 	Illegal, public education is mandatory. Only children with special needs can be homeschooled under strict government control. 	Less than 100 families 	███	1 2 3
Croatia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Cyprus 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Czech Republic 	Legal under restrictive conditions by temporary experimental law for children aged 5-12 	Unknown 	███	1 2 3
Denmark 	Legal as alternative to the mandatory public school system. 	1% of students 	███	1 2 3
Estonia 	Legal under restrictive conditions, only allowed for exceptional cases. 	Unknown 	███	1 2
Finland 	Legal as alternative to the mandatory public school system. Written and oral examinations to check on progress are mandatory. 	Unknown 	███	1 2 3
France 	Legal under restrictive conditions, only allowed for some special circumstances including health reasons. Inspections are mandatory every year.	Unknown 	███	1 2
Georgia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Germany 	Illegal, public education is mandatory with the only exception for foreign families that live in the country for a short time. 	Virtually no homeschooling*	███	1
Greece 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Hungary 	Legal 	Virtually no homeschooling*	███	1
Iceland 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Ireland 	Legal, homeschooling is allowed by the constitution. 	Unknown 	███	1
Italy 	Illegal, elementary education is mandatory without known exceptions. 	Unknown 	███	1
Kazakhstan 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Latvia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Liechtenstein 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Lithuania 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Luxembourg 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Macedonia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Malta 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Moldova 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Monaco 	 	 	███	
Montenegro 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Netherlands 	Legal under restrictive conditions, only legal for parents that can not find a public school fitting their beliefs in the area. 	Virtually no homeschooling*	███	1 2
Norway 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Poland 	Legal, no indications for a mandatory public school system. 	Unknown 	███	1 2
Portugal 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Romania 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Russia 	Legal since 1992, law sometimes ignored and not made legal. 	Unknown 	███	1 2
San Marino 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Serbia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Slovakia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Slovenia 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Spain 	Legal under restrictive conditions, parents are allowed to homeschool if they pass a test by the authorities. 	Unknown 	███	1 2 3
Sweden 	Legal, testing and permission required; sometimes ignored and not allowed 	200 families—half legally 	███	1 2 3 4
Switzerland 	Legal under restrictive conditions in most cantons (states). 	Virtually no homeschooling*	███	1 2
Turkey 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
Ukraine 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1
United Kingdom 	Legal as alternative to the mandatory public school system. 	Unknown 	███	1
Vatican City 	No indication for educational laws to exist were found. 	 	███	
== North America ==
Country 	Status 	Statistics 	Map	Sources
Canada 	Legal under regulating conditions (Alberta - regulation, British Columbia - registration, Manitoba - permit, Newfoundland - permit, New Brunswick - permit, Northwest Territories - regulation, Nova Scotia - regulation, Ontario - regulation, Prince Edward Island - regulation, Quebec - permit, Saskatchewan - permit, Yukon - regulation, 1xgreen/6xyellow/5xorange)	About 20,000 	███	1
Cuba 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2 3
Greenland 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Guatemala 	Illegal, public education is mandatory without known exceptions. 	Virtually no homeschooling*	███	1 2
Mexico 	Legal, compulsory attendance laws unclear. 	Unknown 	███	1 2 3
United-States	Legal under regulating conditions, varies by state. 	Around 1 million 	███	1
 	Incomplete list of prominent countries by population, add extra countries on demand. 	 	 	
==New Zealand==
In New Zealand you must fill out an "Exemption Form" in order to homeschool your children. As long as you know how to fill out the form (your local support group can help with this) it's easy and you can have your children out of school and back into the home learning environment within weeks.
However your children will then be assessed by the Education Review Office, or ERO. The ERO officer will go through your children's work and if you have not satisfied their requirements they can revoke your exemption form, meaning that (legally) you must send your children back to school.
Of course because there are so many homeschoolers and so few ERO officers, they will only visit about once or twice in the child's educational timespan. Indeed some families get missed out altogether.
Some parents, such as the self-titled "unschoolers"--who aim to teach only what the child wants to learn rather than forcing a broad spectrum--continue without an exemption form after their inevitable disqualification (due to inadequate range of education), or begin teaching without one, as their teaching methods cannot possibly meet the ERO's standards.
So far, none of these families have (reportedly) been caught doing this, so the repercussions are unknown (although the law makes them sound harsh), but even without a track record of punishments homeschooling without an exemption is not recommended.
Costa Rica
Illegal, public education is mandatory without known exceptions. More information: 
☀https://homeschoolingcostarica.org