The following device is generally used in washing machines to dispense water and cleaning solution in the right amounts whenever needed. How does this device work?
The device in picture most likely is a monostable solenoid valve. A controller will command to open the valve for a specific duration. The design makes an assumption that there is sufficient water line pressure. The duration determines the amount of water or cleaning solution that is dispensed.
Note: If the water line pressure drops, most modern day washing machines will indicate an error. You can test this by opening all water outlets in your house and then starting the washing machine.
To add just a little to the answer from Mahendra Gunawardena, that device contains an electromagnetic coil and nearby a piece of iron that is connected to a valve mechanism. When electricity is sent to the coil, it becomes a magnet and draws the iron bit towards it. This opens the valve, and water can flow. A spring is furnished in the mechanism so that when the electricity is shut off, the spring pushes the valve closed again and the water flow is cut off.
These valves can be designed to either OPEN when electricity is turned on, or CLOSE when it is turned on, and they can also be built without a return spring in such a manner that they will stay open or closed until another pulse of electricity is sent to the coil, and the valve "flips" from one state to the other.
They can also be designed so that the water pressure in the pipe furnishes the force which urges the valve shut (or open) when the electricity is off.
It is also common for such solenoid valves to have clever fluid passages in them which use part of the water pressure to pop the valve all the way open when the coil is turned on. This minimizes the amount of electrical power required to make the valve open or close; these are called piloted valves.
They are many different type of solenoid valves. direct acting, pilot-operated, two ways and many more. Below is an image showing the basics internal of a solenoid.
At high level there are normal close and normally opened a solenoid. Below is the high level mechanisms of a normally closed solenoids
Inside the valve we have the armature. The solenoid is placed over this and completely surrounds the armature so that it’s at the center of its magnetic field. Inside the cylinder of the armature is the plunger and spring.
The spring pushes the plunger down in a normally closed type valve. Because the plunger is pushed by the spring, it will sit in the down position to close the valve indefinitely. But, if the coil receives an electrical current then it will generate an electromagnetic field and this magnetic field passes through the plunger and will cause it to move upwards against the spring therefore opening the valve
Once the electrical current is stopped then the magnetic field disappears and the spring will force the plunger down again to close the valve.
Below is the high level mechanisms of a normally opened solenoids.
With normally open valves we again have the coil sit around the armature but this time the spring pushes the plunger in the upwards position so that the valve is always open unless the solenoid coil is powered.
If we then pass a current through the coil it again creates an electromagnetic field but this time the field pushes the plunger instead of pulling it. When the plunger is pushes it will close the valve and stop the flow of fluid in the system.
When the electrical current is stopped the spring will then force the plunger back into the upward position and open the valve again.
To learn more about solenoid valves check out How Solenoid Valves Work. Additionally. How Solenoid Valves Work - Basics actuator control valve working principle video might be useful too.