A regularity theory for optimal partition problems