This is a simple guide, so I'm going to simplify. Let's say that the reason you are compressing is because you want to reduce the dynamic range of a vocal or instrument because it is too loud in some parts, too quiet in others. You don't want the signal to sound obviously compressed.
I'll assume that you are using a modern compressor with a full set of controls. Some vintage and vintage-modeled compressors don't have all of the controls of a modern unit or plug-in. There's nothing wrong with that on the right occasion, but for the full compression experience having all of the relevant controls is best.
You should probably set the other controls to commonly-useful values, such as these...
These settings should get you started. Now let's look at the attack.
What you want is for the compressor to respond as soon as the signal rises above the threshold level. So the attack time should be as short as you can get it without unnecessary side-effects.
Let's take the case of a picked acoustic guitar. This has very rapid transients. So you can start off with the attack at say 100 milliseconds and reduce it from there. Keep reducing it until you notice that the starts of the notes sound odd. Then back off a little.
Congratulations, you have set the attack time.
What about a snare drum? That also has a rapid transient. Again you can reduce the attack time until the very beginning of the sound doesn't sound right. Then back off to let the transient through.
With a vocal, there isn't often such a rapid transient, so you have more leeway. But funny things can happen with extremely short attack times so once again start off with a longish attack and reduce it, but listen very carefully because the side effects of too short an attack are harder to hear in a vocal. You will however find that there is quite a wide range of values that sound perfectly good.
One further point that is worth noting is that it is quite possible to set the attack time so fast that the compressor cuts into individual cycles of the signal's waveform at low frequencies. This doesn't only happen as the signal rises above the threshold, but constantly as the level changes above the threshold. It's worth listening out for, and the remedy is to lengthen the attack.
Remember that everything is judged by ear. So whatever anyone tells you is the right attack time, or the right way to set the attack time - including me - listen carefully and trust your own judgment. That's the best way to learn.
P.S. Achieving a natural sound is just one way to set the compressor. There are other times when you want to hear the compression effect. That's going to be another article for another day.
Come on the FREE COURSE TOURGreat home recording starts with a great home recording studio. It doesn't need to be expensive if you know how to select the right equipment for your needs.