When you edit your photos or other graphical data with photoshop, sometimes you come to a point when you need to use the eraser tool to delete something. But there are sometimes situations when you delete something, close the file and open it again after some time just to realize that you now need the object you've already deleted again. With non destructive editing you can hide pixels from a layer instead of deleting them permamently. Visually the effect is absolutely the same, but you can always undelete the pixels. To illustrate the usage of layer masks, consider an image like this:
As you can see we have a circle on one layer and a rectangle on top. But now we face the task of deleting the part of the rectangle that overlaps with our circle. The default solution that comes to mind would be
CTRL+CLICK on the circle layer (this selects the circle), select the rectangle layer and press
DEL to delete selection. It's quick and easy. But what if we need the rectangle in the future? Sure we can always draw another, but i'm talking about more complex situations when there's no rectangles involved :) I'm talking about situations when you can't just paint back what you've deleted. Here's where layer masks come into play. Select the rectangle layer (because that's the layer we want to delete stuff from) and press the layer mask button

in the layers panel. You'll see a layer mask appear next to the layer thumbnail:

The white rectangle next to the thumbnail is our layer mask. And the black borders around it show that the mask is active (selected). OK now let's try to understand what it's for. A layer mask is exactly the same size as your layer. You can do whatever you want with it, but you can't see the mask itself. Imagine the mask as another layer on top of the current layer, but if you paint on it - parts of the original layer either appear or disappear. It's difficult to imagine, but hopefully you'll undersand when i explain it further. If you click on a layer thumbnail and try to paint something - it will act as you'd expect, but if you click on the mask thumbnail and then try to paint on the layer - you'll see that a part of that layer either appears or disappears. Let's consider the very basic example: select the mask and fill the layer with black. As we already know, the layer itself won't be filled, but wait i minute ... where did the rectangle go? It disappeared completely. That happened because we've filled our entire layer mask with black color and black means
hide. Fill it with white again and you'll see the rectangle appear again. What just happend is you've undeleted your rectangle! And you would have been able to undelete it even after closing the file. But that's just a very simple example so let's go deeper into exploring this technique. What would happen if we selected some of the rectangle, selected the layer mask and then filled it with black? The selected part would disappear. So the conclusion is: whereever we paint with black on a layer with its mask selected - that part of the layer gets hidden. This is enough for us to solve the problem with our current image. If you haven't added the mask to rectangle layer yet, do it. Now select the circle layer as we did before (
CTRL+CLICK on its thumbnail). Then, click on the rectangle's mask, set the foreground color to black and fill the selection. The retangle part, overlapping the circle has now disappeared. And the best part is we can always bring it back. We can either fill the entire layer with white when the mask is selected or we can paint over the part that we've deleted. If that's the only thing we've masked out then, obviously, the easiest way is to simply fill the entire mask with white. If you fill certain parts of the layer with black when the mask is selected, you'll notice that the preview of your fill is reflected on the layer mask tnumbnail. So you can always see if anything is masked on the layer or not.
Has your head already exploded? I hope not, since that was relatively easy and i hope i've managed to explain it good enough. Now let's go even deeper into layer masks. What we've talked about so far is called a binary transparency. Binary transparency is a type of transparency where an area can be either transparent or opaque. And we've learned that black fill enables the transparency of the mask, thus hiding the contents of that area in a layer and white fill disables it, unhiding the contents. But there's a more powerful transparency type, called an alpha transparency. As you might already know, computer screens make the colors from 3 main components: Red, Green and Blue. They mix different values of these components to get the final color. Every component can have its value from a range of 0 to 255. If, for example, all the components are 0 - the final color is black and if all the components are 255 - the final color is black. If the red component is 255 and the others are 0 - the final color is red. That's easy. But when working with transparencies, there's also another component involved - the alpha channel. You probably already know how to change the layer's opacity, don't you? You can set it from 0% to 100%. Alpha channels are very similar with two main differences: a) they can change opacity of only a part of the layer; b) their values are 0 to 255 instead of 0% to 100%. Our layer mask also supports alpha transparency and we can use it to make interesting effects. We can paint on the mask with different shades of gray and this will give us different values of transparency on that areas. For example we can take 50% transparent black brush and draw on the rectangle. That would make the area we've drawn on 50% transparent. And we can make another part of the same layer 80% transparent etc. As you can see, we can use different transparency values for virtually each pixel on the layer and you can't do that with layer's opacity. Normally when we want to delete a complex objext (for example separate a person from a background), we use a soft eraser brush to get soft edges. We can do exactly the same with layer masks. Only instead of an eraser, you'd use a black soft brush. Now let's see a quick demonstration of how you can set multiple opacities on the same layer. Quickly create a document like this:

Here we have a red background and a blue fill with a layer mask on top. Now take the gradient tool

from the
tools palette, select the blue layer's mask and choose a foreground to background gradient type. Press D for default colors. You can see that when you select the mask, your colors change to black and white (or white and black), but press the D button just to make sure we have the same settings. Now draw a gradient from top to bottom. The result will be like this:

As you can see, the layer mask thumbnail shows us that we have a white to black gradient, but the actual gradient showing is blue to red. This is because the layer mask only shows the transparency map in shades of gray. The darker the area on the thumbnail - the more transparent the area is in the actual picture. So what we've done here is we've created a gradient mask over our blue layer. The further we go down the layer - the more transparent it gets. And finally it becomes completely transparent. That's why we can see the red background and get the impression that we've used a blue to red gradient. You can use this technique if, for example, you want to blend a sky with water or ground. You'd select the bottom part of the sky and fill it with the same gradient. This would make the bottom of the layer fade out just like we did with the whole blue layer in the example.
I hope it wasn't too difficult to understand and you've learned something new today.