It is usually best to leave the fruits on the plant for as long as possible as nature will do a better job than human intervention - you could pick too soon and instead of having nice sweet squashes that store well over winter, they may rot off on the windowsill and never ripen at all
Don't forget that most fruits have developed sweet tasty flesh to encourage birds and animals to eat and help disperse the mature seed. If a fruit has undeveloped seed, that process has been interrupted. The fruit is no longer there for purpose, so it rots (compare a courgette to a mature marrow)
When will you know? Actually, with great difficulty unless you are familiar with the squashes you grow. The easiest way is to let the first frosts kill off the plants, then the squashes can be harvested. Some will be ready to use right away, some might need to be stored so that the flavour of the flesh improves (you'd know if it was one of yours as the seed instructions tend to point this out in the after care) But for good winter storage, the skins will need to be "cured" - that means letting the outer skin harden off. Good ventilation, dry and a sunny location over a couple of weeks or more (you should not be able to push your finger nail into the skin) then store somewhere cool and dry.
Not all fruits store well so do make a point of checking on them periodically and removing any that look soft and mouldy (if not too far gone, you can cut those bits off and use up the solid flesh that is still fine - nothing worse than saving one of your favourite squashes for later and then for it to keel over)