A kilonova as the electromagnetic counterpart to a gravitational-wave source