I have an earphone plug having four colored wires (gold, red, green, blue) and a jack with four connectors:
I need to know which colour should be soldered to which connector.
You can not rely on color codings. There may be standards but not every manufacturer may obey them.
This is how you test the correct wiring:
Test continuity between the wires
This step finds out which wire pair is part of the speaker coils. This step might be not necessary when it's obvious which pair goes to which speaker. In the shown case it's not though.
The headphone wiring might look like one of these:
The four wires indicate that it corresponds to right schematic. Though unlikely, note that L1 and R1 or L2 and R2 (or even another combination across left and right) might be joined inside the headphone leading to a configuration that is equal to the schematic on the left side.
The earphones or speakers will have internal resistance (impedance), they can be simplified to resistors. Usual values range from 30 Ohm to 700 Ohm or even 10 Ohm for tiny earplugs.
Use the resistance or continuity test function of your multimeter to test the resistance. The continuity test should display the resistance and beep/buzz when the resistance is low (lower than 50 Ohm, depending on the multimeter). The resistance function should show a resistance that is suitable for the headphone impedance.
For example, a 120 Ohm impedance should measure with about 120 Ohm. Assuming L2 and R2 are joined within the headphone the resistance (continuity) should measure lower than 1 Ohm.
In the case show in the question each color refers to L1, L2, R1 and R2 (L, R and C) respectively.
Probe every combination of two wires with the two test leads of the multimeter. When there's continuity (or low resistance) the two wires are part of the coil of one speaker. The other two wires must be part of the other speaker. They should have continuity too. If they don't have continuity, the speaker might be broken or the wiring is more complex (unlikely and not covered here).
As mentioned above there might be a configuration with four wires but internal connection that relates to the left schematic (L2 and R2 might be the same as C). In that case one pair might measure 120 Ohms and another might measure 240 Ohms (since the two coils are in series and the resistance is doubled) while another pair measures lower than 1 Ohm.
Refer to the manual of your multimeter how to use the resistance or continuity test function.
Probe each pair with a (audio) signal
To find out which pair belongs to which side you have to probe them. This step might be not necessary when it's obvious which pair goes to which speaker. In the shown case it is necessary though.
Signal sources might be
A signal or function generator
The signal generator may generate a audible (100 - 20.000 Hz) sine wave.
With the signal generator, you should not exceed the power ratings of the headphone (current and voltage). Starting with 0.5 V should be fine for most headphones.
The continuity test function of a multimeter (not reliable)
Though the continuity test outputs a small constant current and this is not a proper audio signal, it will cause a click sound in the speaker. Tapping the wire pair with the continuity test should reveal which pair belongs to which side.
A audio source (soldering the wires to the plug off chance)
You might just solder the wire pair to the corresponding leads and try it out. If the channels are reversed swap the pairs.
Figure out the plug connection
Either refer to the schematic of the plug or, in doubt, use the continuity test function of a multimeter to test which ring belongs to which lead.
Connect the wires
Note that this section only refers to standardized connectors. Properitary equipment might not confirm to any standard. For example, the four contact (TRRS) plug shown in the question is not usual for headphones. In doubt the amplifier signals should be tested too (not covered here).
The plugs might come in different variations. The contacts are called tip, ring and sleeve. According to the configuration of these contacts the plugs are called TS, TRS and TRRS.
Usually the left channel is the tip, the right channel is the ring and the common (or ground) is the sleeve.
In case of TRRS plugs (this is what the question shows) the connection is a bit tricky since there are different standards allowing a microphone or even video connection.
3.5 mm TRRS (stereo-plus-mic) sockets became particularly common on smartphones, and have been used e.g. by Nokia since 2006; they are often compatible with standard 3.5 mm stereo headphones. Two different forms are frequently found, both of which place left audio on the tip and right audio on the first ring (mirroring the configuration found on stereo connectors). Where they differ is in the placement of the microphone and return contacts.
Most of them have in common that, like with TRS, the tip is left and the first ring is right.
Refer to a full list of possible connections in Wikipedia. The other two standards are not mentioned here because they won't be compatible with standard stereo/mono plugs at all.
For headphones without a microphone it should be fine to connect common/ground to ring two and sleeve.
Asking how to use a multimeter, a signal generator or how a speaker works is beyond this question. Use the search function or a search engine. There are plenty resources out there.
Further, testing how non standard equipment works is beyond this question and should be a well defined, separate question.
Are you making an extension cable? If so, simply insert the plug into the jack and test continuity between each conductor and the various connections on the plug.
Solder each conductor to the connection that has continuity.
Very quick and easy.
[Edit] I give a detailed description how to repair damaged headset connectors in another question. Terminate Headset Connector