SEO is not illegal. Yet, there are some Black Hat SEO techniques that are illegal and also punished by Google. However, normal White Hat SEO strategies are all legal and appreciated by search engine algorithms.