The pipes aren't well anchored in the wall. When the valve shuts off, the inertia from the moving water quickly being stopped by the fill valve moves the pipe and it hits something. This is called water hammer. You can install a waterhammer arrestor. They come in various types, and from various manufacturers.