What refers to an open set of objects or an unlimited amount of possibilities.
What is it?
What do you like to do?
Which refers to a closed or more specific set of items, like a group. Which can refer to animals or objects but never to people, which uses 'who' instead.
Which book is your favorite?
Sometimes they can be used interchangeably.
What/Which animals do you like?
Personally, I usually use 'what' when referring to something relative or general, while I use 'which' when presented with options.
'What is your favorite color?'
'Which is your favorite color? Red, green, or blue?'
This was a surprisingly tricky question. Hope this helps!