How does keras (TF) know how to differentiate my custom loss?
keras will differentiate automatically by looking at the graph of operations you use as part of your custom loss function. Since you are using only operations that keras "knows about" aka that exist as operations in TensorFlow keras will automatically a graph of operations to backpropagate against.

If you however have a custom loss function that uses an operation TF doesn't know, you'd have to write a custom operation so that TF knows how to backpropagate against it