Hard water contains calcium and magnesium ions. Therefore, in hard water, soaps get precipitated as calcium and magnesium salts which being insoluble stick to the cloth as gummy mass. Hence, soap cannot be used with hard water.
On the other hand, calcium and magnesium salts of detergents are soluble in water, so they easily form lather with hard water.