The best time is right after they finish flowering. Lilacs set the buds for next year’s flowers during this summer and those “latent buds” spend the winter in the lilac and open in spring. Pruned right after flowering promotes branching out and filling in sparse areas and the new growth will have time to set buds before winter. If you really need to prune later, it will not harm the lilac but you won’t get the flowering you’d expect since there won’t be time for flower buds to form.