What do all those different valves do? I'll try to keep my answer short.
First of all, there are a large number of tanks both inside the pressure hull and outside the pressure hull of a sub that can contain high pressure air, ambient pressure air, sea water, fresh water, fuel oil, lubricating oil, etc. Those tanks feed pipes that run throughout the sub and through its pressure hull and all those valves are used to convey the tank contents via pipes to wherever they are needed.
For example, one set of tanks, pipes and valves are used to move sea water back and forth along the length of the sub and from side to side inside the sub, in order to get it to float level when submerged and when on the surface.
Another set of tanks, pipes and valves are used to get the sub to float on the surface at the proper height above the water level, regardless of how much the sub weighs.
Yet another set of tanks, pipes and valves is used to set the weight of the sub so that when it must suddenly submerge, it will be neutrally bouyant (as close to that as possible) when the air keeping it afloat is released.
Another set of tanks, pipes and valves is used to blow air into the flotation tanks to bring the sub back to the surface after a dive.
In the engine room, there will be valves in the intake pipes feeding outside air to the diesel engines and valves in the exhaust pipes carrying exhaust out of the engines and out of the sub's hull. There will also be valves to conduct cooling water from outside the hull to the engine blocks and valves to feed fuel oil to the engines from a variety of different oil storage tanks in the sub.
Note also that the sub has a complete fresh water plumbing network to furnish water for washing, cooking and drinking from the distillation machinery to all the different compartments in the sub- and a sewage system to carry away all the waste water as well. This means still more pipes with valves in them.
It is also common for a single pipe to have several valves in it so if one valve fails or a portion of the pipe blows up and starts to leak, there will be some other means available to shut off the pipe and save the sub from sinking.