Illegal ?
Well you won't go to jail for buying links 😀 Sorry that made me smile,
Buying links is fine, there is no difference between a advert for your site (which is paid for) and a link. The difference though is all adverts should have the tag rel="nofollow".
I suspect though you mean buying follow links and doing this in bulk in order to gain in SERPS. This is of course frowned upon by search engines as its a clear signal your attempting to manipulate your position by posting links to your site. It is also very easy to spot as suddenly a load of links appear within a short space of time. Your backlink profile would see a spike which would trigger it to be looked at more closely.
In the old days this would mean a human inspecting your backlink profile, however google have since matured and can do this automatically now and often you will see your position drop instead of rising because of a penalty automatically applied to your site.
Adverts on the other hand are posted for traffic and therefore are typically ignored by google from a ranking point of view. You are welcome to post as many adverts as your budget allows but make sure they are of the no follow type.