The Database gave you a good answer. The art of sacrifice based on two things : first you need a calculation that proves there is an attack on going and second you need a piece coordination to support and continue the attack. If your position doesn't have this kind of consolidation then it's not worth unless your last name is Tal.
Now, in your question how you will know when it's the time to do it, that depends on your positional understanding and how you will work on tactics. A good exercise for you here, is to sacrifice the bishop and play black pieces trying to defend. Any time you want to sacrifice a piece, just play the opposite side and find the best solution to defend against this and why. After many exercises, you will have a basic idea how and when.
What is a common position when it's good to sacrifice after the other player extends pushes their castled pawns to break a pin? I was playing in a Ruy Lopez as white and thats what happened. However I felt like I couldn't do it because their queen had a direct diagional into the position. I put the game in Chessbase and I was right that no variation would help help. In fact the only good suggestion was forgot the pin and trade bishops and get a big center. Heres the game