A small degree of water retention is normal in pregnancy. The reasons are described well on the American Pregnancy Association website.
Noticeable swelling of feet and ankles in late pregnancy is also common and occurs because the weight of the uterus puts pressure on the pelvic veins and the vena cava. The vena cava is a large vein on the right side of your body which transports blood from your legs and feet back up to your heart. Pressure on this vein slow down the blood flow, making blood collect in the lower legs, feet and ankles. The pressure on the walls of the blood vessels in these areas forces fluid out of the vessels and into the tissue spaces. This takes the form of swelling. If you push a finger into this swelling, it will often leave an imprint.
Some basic advice on dealing with this problem (and also signs which must be reported to a doctor) can be found on the MSD Manual website. For more detailed advice on helping this problem, the 2010 edition of the Waterfall Diet book (published by Piatkus Books) has a chapter on water retention and pregnancy.
Most important of all, when you lie down, try to lie on your left side in order to take some pressure off your pelvic veins.