Those wings are called S-Foil Historically, S-foils had been developed to address overheating issues on wing-based starfighters.
Because of the proximity of engines and weapons systems to narrow wiring that fit inside the thin wings, an excess of heat could cause mechanical meltdowns that would be devastating to the capacity of the fighters to function.
also They [X-wing] had two pairs of wing-like strike-foils, or S-foils, mounted at the rear of the craft on opposite sides.
The foils on each side locked in place flush against each other; during combat, however, the foils were folded out to increase the spread coverage of the laser cannons
mounted at the tips of the foils.
This gave the craft its distinctive "X"-like appearance when viewed from the front or rear.
The cannons on some earlier models could not be fired with the S-foils in locked position, perhaps as a safety feature.
During hyperspace travel, the S-foils remained locked to conserve energy.
T-65 X-wing on starwars wikia.
scifi.stackexchange.com/users/45/DavRob60
For an in-world explanation, I would posit the theory that the wings close for normal travel and hyperspace for the purpose of strength and stability in the wings and they open during combat for two reasons.
First is that by spreading the blasters apart they give it a greater field of fire and greater chance of hitting the enemy.
Second, by moving the engines a bit further apart it could aid in making the craft more maneuverable and better able to change its angle of attack with less thrust.
scifi.stackexchange.com/users/1551/BBlake
I'm seeing some suggestions about increased maneuverability.
Well... no good can really come of applying real-world science to Star Wars.
Case in point, get ready to fall asleep:
Aerodynamically speaking, the change in configuration would have a negligible effect on maneuverability or, if anything, could make the X-wing LESS maneuverable when flying in an atmosphere.
In sciency terms, maneuverability is the inverse of stability; if a plane is more stable, it's less maneuverable, and vice versa.
So first we look at the TOP wings.
If we look at the design of most real-world private and commercial airplanes, the wings are constructed with a slight upward angle--referred to as "dihedral"--when viewed from the front.
This is the greatest contributor to stability on the roll axis of an airplane because the inward tilt of each wing's lift vector tends to roll the plane back to a wings-level attitude.
Next look at the bottom wings.
Since someone's already mentioned the Harrier, let's use that
: The Harrier's wings are built with a heavy downward angle--or "anhedral.
" This makes the aircraft extremely maneuverable, which implies a huge loss of stability due to the outward cant of the lift vectors.
The result seems like an equal and opposite effect on maneuverability: the dihedral top wings negate the effect of the anhedral bottom wings, leaving us with an unchanged situation.
BUT--without jumping too far down yet another aerodynamic rabbit hole--
the addition of another wing plane adds a complication suffered by real-world biplanes, in which the top wing tends to undermine the lift of the bottom wing, leading in THIS case to a net increase in stability--in other words, a net decrease in maneuverability.
Ergo, under the shackles of real-world science, the change in wing configuration has, at best, no effect, and at worst, a negative effect on the X-wing's maneuverability.
Best to just drink the koolaid.
scifi.stackexchange.com/users/57818/BuzzKillington
I've read that the closed position was required for atmospheric flight.
The Z-95 was the X-Wing's precursor.
There were likely many design choices in the Z-95
that made it cheaper to design the X-Wing as a transformer then to redesign it from scratch.
scifi.stackexchange.com/users/2746/Hand-E-Food
The reason may be for accuracy.
According to the X-wing series by Michael A. Stackpole, the weapons are calibrated to a certain distance (usually a few hundred meters).
It seems to me that it would be most effective for these four blasters not to fire in two closely grouped pairs, but to be more spread out.
Additionally, the purpose of the wings coming together likely makes atmospheric flight easier (more airplane-like), so the reason for them being separate in attack position may be less significant than the reason for them being together in standard flight mode.
scifi.stackexchange.com/users/4771/gobernador
Landing?
As they are never shown flying in the atmosphere in a lift generating motion.
They seem to take off more like a harrier(with endless fuel).
So never any need for them to generate lift.
Pretty good question.
I think the model builders just came up with something cool and lucas said ok.
scifi.stackexchange.com/users/6093/joe
Wings closed: all energy to engines, weapon capacitors empty and cool.
Wings being closed is for landing and storage purposes as well, and maybe wings closing when weapons are disabled is an automatic feature e.g 'Navigation mode' usually engaged prior to landing/docking.
Wings open: provides cooling for weapons energy capacitors which are stored in the wings, the capacitors and Taim&Bak lasers produce massive amounts of heat so both the weapons and capacitors are kept at a distance from each other to better dissipate heat and radiation.
Since the X-wing didn't see combat every time it flew, its wings remained mostly in closed position, seldomly activated for combat.
How's that for a theory?
scifi.stackexchange.com/users/53328/Mikhail
In real aerial dog fights, the wing cannons of an aircraft are calibrated to have the bullet´s trajectory converge at a certain distance before the aircraft.
This is where the ideal shooting distance is indicated in the gunsight.
Moving the guns further apart in fighting configuration gives the engineers a wider range to tilt the guns to form a firing cone.
From a movie makers point of view, unfolding the wings to attack gives the spaceship a more menacing, warlike appearance, similar to a japanese judoka assuming fighting stance.
scifi.stackexchange.com/users/71691/Sparrowhawk
The X-wing fighters can change their wing configuration, from the X shape during battle to flat wings (the wings on the same side fold together) during FTL travel.  I'm guessing that the real world explanation is something like, "it looks cool". But what is the in-universe explanation?  There is no (little) resistance in space, so wing configuration wouldn't make that much difference. I can't see it making much change to performance, e.g. in angular momentum, either.
Why do X-wings change wing configuration?
scifi.stackexchange.com
<star-wars><spaceship>
scifi.stackexchange.com/questions/6001
scifi.stackexchange.com/users/143/Reinstate Monica - Goodbye SE
Wings closed is for landing and storage purposes and wings may close when weapons are disabled is an automatic feature. Wings open provides cooling for weapons energy capacitors which are stored in the wings, the capacitors and Taim&Bak lasers produce massive amounts of heat so both the weapons and capacitors are kept at a distance from each other to better dissipate heat and radiation. During hyperspace travel, the S-foils remained locked to conserve energy.   There are a number of theories that speculate over why the wings open and close in the forms. Theories include Closed wings are better for hyperspace travel, landing, sotrage while open wings are better for weapons accuracy, better weapons cooling capabilities etc. All in all there is no in-universe explanantion. There are only theories.
The theory could be that the wings are closed for normal hyperspace travel There are written documents that state the closed position was required for atmospheric flight The reason may be for better accuracy. Closed wings may be better for landing and storage based purposes Wings open: provides cooling for weapons energy capacitors