It is not illegal, but it won't be profitable
Basically, think of it this way; when "mining" you put your device onto a network called the Blockchain. The purpose of the Blockchain is to a) keep an updated list of every transaction that has ever taken place in the history of Bitcoin, and b) check the math of any new transactions to make sure they're valid. As more Bitcoin exist, the algorithms for every transaction get more complex to compensate. Because of this, computers that are part of the Blockchain will need to be more powerful to solve them.
As for where new Bitcoins come from, there are a limited amount of Bitcoin that can be minted, which are, again, controlled by the network. By adding your computer to the Blockchain and helping it moderate transactions, it rewards you with newly minted Bitcoins proportional to the amount of work you did